• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Format BigDecimal

 
Ranch Hand
Posts: 150
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I need to format a java.math.BigDecimal with any number of integers and any number of decimal places, including zero decimal places, with comma separators.

Results like
123.456
123
0.123456789
123,456,789.0123456789

Remove insignificant zeros following the decimal point.

I'm using JDK 7.

Suggestions?

Thanks,

Lou
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The NumberFormat class lets you specify formatting. One of its format methods takes an Object as a parameter. That Object can be a BigDecimal.
 
Lou Pelagalli
Ranch Hand
Posts: 150
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was easy, thanks Internet Detective Marshal Jeanne!

Posting my test code and result below in case anyone cares.

Note that the default number of decimal places for NumberFormat.getInstance() is 3.

That can be changed with NumberFormat.setMaximumFractionDigits(int).

In my case
BigDecimal bd = new BigDecimal("00000123456.7891234541000");
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(bd.scale());

Thanks,

Lou



Result
fieldValue.toString() = '123456.7891234541000'
fieldValue.toPlainString() = '123456.7891234541000'
fieldValue.toEngineeringString() = '123456.7891234541000'
Formatted bd = '123,456.7891234541'
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome. I gave you a cow for taking the time to post a good example of what you learned.
 
Lou Pelagalli
Ranch Hand
Posts: 150
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you again Jeanne!

I have a cow!
 
Ranch Hand
Posts: 127
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:The NumberFormat class lets you specify formatting.  One of its format methods takes an Object as a parameter.  That Object can be a BigDecimal.



Hi Jeanne,

I also have the similar question to your answer above.

I see lots of programming book use NumberFormat class's format() method to format the BigDecimal object.

For example,




I check the documentation on Oracle's website. format() method in NumberFormat class only takes long and double as 1-argument. But why is BigDecimal is also valid as argument in format()?

Thanks!
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Duplicate post, already answered here: https://coderanch.com/t/690845/java/NumberFormat-format-BigDecimal-class
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic