Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Display of BigDecimals

 
Richard Ellison
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • 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
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic