File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Display of BigDecimals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Display of BigDecimals" Watch "Display of BigDecimals" New topic

Display of BigDecimals

Richard Ellison
Ranch Hand

Joined: Jun 04, 2008
Posts: 51
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?

Paul Clapham

Joined: Oct 14, 2005
Posts: 19856

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

Joined: Jun 21, 2008
Posts: 423
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:
It is sorta covered in the JavaRanch Style Guide.
subject: Display of BigDecimals
It's not a secret anymore!