Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

formatting number

 
Amol Juvatkar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,
i want to display number as format 99,99,999.99

I am using DecimalFormat("##,##,###.00")

but sill it is showing output as 9,999,999.99

kindly suggest me if there is another solution.

my code is like this,



thanks,
Amol
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

See the Java Source for DecimalFormat(String) Parameter Constructor. It depends on the Locale Object.




So, change your locale and try it.
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From ye old JavaDoc -

The grouping separator is commonly used for thousands, but in some countries it separates ten-thousands. The grouping size is a constant number of digits between the grouping characters, such as 3 for 100,000,000 or 4 for 1,0000,0000. If you supply a pattern with multiple grouping characters, the interval between the last one and the end of the integer is the one that is used. So "#,##,###,####" == "######,####" == "##,####,####".
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not going to help. Read the DecimalFormat documentation, specifically this excerpt:


The grouping separator is commonly used for thousands, but in some countries it separates ten-thousands. The grouping size is a constant number of digits between the grouping characters, such as 3 for 100,000,000 or 4 for 1,0000,0000. If you supply a pattern with multiple grouping characters, the interval between the last one and the end of the integer is the one that is used. So "#,##,###,####" == "######,####" == "##,####,####".


It would appear that what you hope to accomplish isn't possible with DecimalFormat. The last grouping in the format is used for all groupings, if you want them to be different I guess you'll have to find one or roll your own.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree you aren't using a standard thousands separator format.

If all else fails, try integer division and remainder operators. But make sure you don't stray outwith the bounds of the int type:-

Or try using the Double class to change the number to a String, and split it into a char[] array, then print them one by one with commas as appropriate. You will have to count the digits in your numbers carefully for both methods otherwise you will get something like, "0,01,234.56."
 
Amol Juvatkar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help.

regards
Amol
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a regex implementation of Campbell's second suggestion:
 
Amol Juvatkar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Alan,

now one more doubt.



with
statement 1 formatting doesn't work.
but
with statement 2 it works properly.

in general both are strings only.
so what may be wrong ??

regards,
amol
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic