Formatting numbers with spaces between them

H

hulkko123

Hello,

I have String containing only numbers and a comma in followin format
nnnnnnnnn,n where n is number 0 - 9.
E.g. the right value would be 121231234,1

I have to convert it into format 12 123 1234,1

How should I code it properly using Java's Formatter classes?
I can code that using loops etc. but I do not want to code such
code. So, anyone know how to do it with formatters. I have to support
JDK 1.4.

I tried a code below, but spaces in the last line does not appear
between
numbers. I know that might approach can also be wrong.
Could someone please, how handle this formatting nicely.

String s = "121231234,1";
double d = Double.parseDouble(s);

DecimalFormat df = new DecimalFormat("## ### ####.0");

System.out.println(String.format("## ### ####.0", d));


// Output is still 121231234,1 but I would like have
// it in form 12 123 1234,1


Cheers!
 
R

Rhino

Hello,

I have String containing only numbers and a comma in followin format
nnnnnnnnn,n where n is number 0 - 9.
E.g. the right value would be 121231234,1

I have to convert it into format 12 123 1234,1

How should I code it properly using Java's Formatter classes?
I can code that using loops etc. but I do not want to code such
code. So, anyone know how to do it with formatters. I have to support
JDK 1.4.

I tried a code below, but spaces in the last line does not appear
between
numbers. I know that might approach can also be wrong.
Could someone please, how handle this formatting nicely.

String s = "121231234,1";
double d = Double.parseDouble(s);

DecimalFormat df = new DecimalFormat("## ### ####.0");

System.out.println(String.format("## ### ####.0", d));


// Output is still 121231234,1 but I would like have
// it in form 12 123 1234,1
I assume that you want this formatting because you are trying to please a
user who is used to seeing numbers in that format.

Different countries and regions have different expectations on the way
numbers should look so I would solve your problem by taking advantage of the
Java support for Internationalization (often abbreviated 'i18n') and
Localization (often abbreviated 'l10n'). Java offers substantial support for
i18n/l10n as described in the Java Tutorial, beginning here:
http://java.sun.com/docs/books/tutorial/i18n/index.html

To illustrate how simple this can make your code, I wrote this short
fragment that solves your problem:

int myInt = 123456789;

double myDouble = 123456789.123;

NumberFormat numberFmt = NumberFormat.getNumberInstance(new Locale("fr",
"CA"));

String formattedInt = numberFmt.format(myInt);

String formattedDouble = numberFmt.format(myDouble);

System.out.println("myInt = " + myInt);

System.out.println("myDouble = " + myDouble);

System.out.println("formattedInt = " + formattedInt);

System.out.println("formattedDouble = " + formattedDouble);

In the code, an int, named 'myInt', and a double, named 'myDouble', are
created and initialized. Then, a Java NumberFormat, named 'numberFmt', is
created assuming that users are in the Locale associated with the
French-speaking parts of Canada. Then, for each number that I want to
display (write to a file, show on a screen, etc. but not use in a
calculation), I apply 'numberFmt' to that number, creating a String
representation of the number. Lastly, the numbers are displayed and you can
see that the formatted int uses blanks as the thousands separator. The
formatted double also uses blanks as the thousands separator and uses a
comma as the decimal point.

All you need to do is use the appropriate values for the Locale you are in
and your numbers will be displayed in the format normally used in your area.

This isn't the only solution to your problem but I think it is probably
easier than the approach you were using.
 
O

opalpa

DecimalFormat uses one width for splitting numbers into segments.

Separately: does your local use "," instead of "." for doubles? That
is your String s is going to thrown an exception in my environment.
Also "System.out.println(String.format("## ### ####.0", d));" does not
use format you are trying to create.

Opalinski
(e-mail address removed)
http://www.geocities.com/opalpaweb/
 
B

Bobby E

Yes, in many European country decimal is , not .
Is it possible to change decimal character from . to , and use Formatter?

Any how thanks for your reply.
 
O

opalpa

Is it possible to change decimal character from . to , and use Formatter?

Yes, look at Rhino's post. If you want to directly change formater's
symbols look into using setDecimalFormatSymbols .

Although I knew different places used different characters for decimal
point in output and input I did not know that Java source code could be
written using them also.

Opalinski
(e-mail address removed)
http://www.geocities.com/opalpaweb/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top