• 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

Display of BigDecimals

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
How do I ensure that a value which is of a BigDecimal datatype always has 3 digits.Meaning-if the value is 5 then the displayed value should be 005.
If 0,then the value should be 000.

Can a BigDecimal datatype be used or should I use another datatype?

Thanks
Richard
 
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
Don't confuse how you store the data with how you display the data. The two concepts are independent. I don't think I would choose BigDecimal to store numbers which are restricted to being integers between 0 and 999; for one thing you aren't using anything after the decimal point so BigInteger would be more reasonable, and you aren't storing "big" integers so a plain old "int" value would be just fine.

As for displaying those values with leading zeroes, consider using a suitably-configured DecimalFormat object.
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Datatype of a value has nothing to do with it's printing/display representation.

If you want to format a value with leading 0, you can do it for example in this way:

Numbers in Java can be formatted using Formatter class, read this:
http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic