1. Member
Join Date
Feb 2012
Location
Norway
Posts
96
Rep Power
0

## Number format

Hi,

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

Here is my code:
Java 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

2. ## Re: Number format

The # symbol displays a digit when necessary, the 0 character always displays a digit.

kind regards,

Jos

3. Member
Join Date
Feb 2012
Location
Norway
Posts
96
Rep Power
0

## Re: Number format

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

4. ## Re: Number format

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

5. Member
Join Date
Feb 2012
Location
Norway
Posts
96
Rep Power
0

## Re: Number format

Great. Thanks.

Heres my updated code:
Java 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

6. Member
Join Date
Feb 2012
Location
Norway
Posts
96
Rep Power
0

## 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:
Java 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:
Java 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?

7. ## Re: Number format

Take a look at the SimpleDateFormat class

8. Moderator
Join Date
Apr 2009
Posts
10,481
Rep Power
16

## 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.

9. Member
Join Date
Feb 2012
Location
Norway
Posts
96
Rep Power
0

## Re: Number format

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.

10. Moderator
Join Date
Apr 2009
Posts
10,481
Rep Power
16

## 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).

11. Member
Join Date
Feb 2012
Location
Norway
Posts
96
Rep Power
0

## Re: Number format

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:
Java 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.

12. Banned
Join Date
Jun 2012
Location
Beijing,China
Posts
34
Rep Power
0

## Re: Number format

Or Handle it as a String.

Java 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

13. Moderator
Join Date
Apr 2009
Posts
10,481
Rep Power
16

## 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•