# Number format

• 06-19-2012, 03:35 PM
asai
Number format
Hi,

I trying to use NumberFormat, but I dont find the correct alternativ.
Anyone that could help?

Here is my code:
Code:

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Result {

public static void main(String[] args) {

NumberFormat nf = new DecimalFormat("### ###,##");

double c = -66181.00;
String sum = nf.format(c);
sum = sum.replace("-", " ");
System.out.println("Resultat: " +sum);

}

}

This displays the amount like this: Resultat: 6 61 81
What I want is this: Resultat: 66 181,00
• 06-19-2012, 03:38 PM
JosAH
Re: Number format
The # symbol displays a digit when necessary, the 0 character always displays a digit.

kind regards,

Jos
• 06-19-2012, 04:25 PM
asai
Re: Number format
Yes, thanks. But how can I get a space between 66 and 181?
• 06-19-2012, 04:29 PM
JosAH
Re: Number format
Quote:

Originally Posted by asai
Yes, thanks. But how can I get a space between 66 and 181?

I'm not sure but if I were you I'd check the DecimalFormatSymbols class ...

kind regards,

Jos
• 06-19-2012, 10:14 PM
asai
Re: Number format
Great. Thanks.

Heres my updated code:
Code:

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;

public class Result {

public static void main(String[] args) {

String format = "#,###,##0.00";
DecimalFormatSymbols forSpace = new DecimalFormatSymbols();
forSpace.setGroupingSeparator(' ');
DecimalFormat formatter = new DecimalFormat(format, forSpace);

double c = -66181.35;
String sum = formatter.format(c);
sum = sum.replace("-", " ");
System.out.println("Resultat: " +sum);

}

}

The result of this:
66 181,35
• 06-20-2012, 03:31 PM
asai
Re: Number format
I am trying another format to.
I have a text field where I write a number that is really a date.
When I type this: 23011971 and change field, I would like the program to insert dots so the field looks like this:
23.01.1971
I have written this:
Code:

private void textDateFocusLost(java.awt.event.FocusEvent evt) {

String formats = "##.##.####";
DecimalFormat df = new DecimalFormat(formats);
String dato1 = textDate.getText();
String dato2 = df.format(dato1);
textDate.setText(dato2);

But I get this error:
Code:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Multiple decimal separators in pattern "##.##.####"
I guess this is because DecimalFormat cant be used this way.
Any suggestions?
• 06-20-2012, 03:45 PM
Norm
Re: Number format
Take a look at the SimpleDateFormat class
• 06-20-2012, 03:50 PM
Tolls
Re: Number format
Those formatters are used for formatting number in fairly standardised ways. They also tend to use the locale to determine locale-specific separators.
They are not intended for formatting your number in every possible way.
• 06-20-2012, 04:06 PM
asai
Re: Number format
Quote:

Originally Posted by Norm
Take a look at the SimpleDateFormat class

Uses this to convert the date from dd.MM.yyyy to java.sql.date for insert into mysql tables.
Also for select from mysql and displaying the date in the same format. This works perfectly.
As I mentioned in my last post, I would like to make a display of this format when I lose focus to the input field.
• 06-20-2012, 04:44 PM
Tolls
Re: Number format
Then use two formatters.
One to go from String to Date (ddMMyyyy) then the other one goes from Date to String (dd.MM.yyyy).
• 06-20-2012, 07:36 PM
asai
Re: Number format
Quote:

Originally Posted by Tolls
Then use two formatters.
One to go from String to Date (ddMMyyyy) then the other one goes from Date to String (dd.MM.yyyy).

Thats it! Works perfect with this code:
Code:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Result {

public static void main(String[] args) throws ParseException {

SimpleDateFormat df = new SimpleDateFormat("ddMMyyyy");
String dato1 = "23011971";
Date dato2;
dato2 = df.parse(dato1);
SimpleDateFormat dato3 = new SimpleDateFormat("dd.MM.yyyy");
String dato4 = dato3.format(dato2);
System.out.println(dato4);

}

}

Thanks for the tip.
• 06-21-2012, 07:37 AM
Ksharp
Re: Number format
Or Handle it as a String.

Code:

public static void main(String[] args)  {
StringBuilder sb=new StringBuilder("23011971");
String new_date = sb.insert(2,'.').insert(5,'.').toString();
System.out.println(new_date);

Ksharp
• 06-21-2012, 10:24 AM
Tolls
Re: Number format
Ksharp's one makes more sense if this is going to be the same every time and you never actually need the date as a Date.
Going from a string to a date and back again is going to be more "work" than simply plonking in a couple of '.' s.