38

I am using BigDecimal to get some price values. Requirement is something like this, what ever the value we fetch from database, the displayed valued should have 2 decimal points.

Eg:

fetched value is 1 - should be displayed as 1.00
fetched value is 1.7823 - should be displayed as 1.78

I am using setScale(2, BigDecimal.ROUND_HALF_UP) but still some places, if the data from DB is a whole number then the same is being displayed !!

I mean if the value is 0 from DB its displayed as 0 only. I want that to be displayed as 0.00

Thanks

informatik01
  • 15,174
  • 9
  • 67
  • 100
user1391730
  • 381
  • 1
  • 3
  • 3
  • 1
    I think you can find your answer right here: http://stackoverflow.com/a/50543/61624 – Daniel Kaplan Jan 25 '13 at 05:02
  • 2
    thanks daniel :)that was too fast reply.. but i am already using the bigdecimal.round_half_up with setscale(2) it works when u have number like 2.12 or 4.2343. when the value is a whole number like just 2 then i want that to be displayed as 2.00 and this is where i am failing !! – user1391730 Jan 25 '13 at 05:05
  • possible duplicate of [How do I format a number in java?](http://stackoverflow.com/questions/50532/how-do-i-format-a-number-in-java) – Bhavesh Patadiya Jan 25 '13 at 06:09

6 Answers6

58

BigDecimal is immutable, any operation on it including setScale(2, BigDecimal.ROUND_HALF_UP) produces a new BigDecimal. Correct code should be

        BigDecimal bd = new BigDecimal(1);
//      bd.setScale(2, BigDecimal.ROUND_HALF_UP);   bd.setScale does not change bd
        bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
        System.out.println(bd);

output

1.00
Evgeniy Dorofeev
  • 124,221
  • 27
  • 187
  • 258
14

you can use the round up format

BigDecimal bd = new BigDecimal(2.22222);
System.out.println(bd.setScale(2,BigDecimal.ROUND_UP));

Hope this help you.

Mr.vicky patel
  • 1,699
  • 1
  • 7
  • 18
corgrin
  • 255
  • 1
  • 2
  • 12
7

To format numbers in JAVA you can use:

 System.out.printf("%1$.2f", d);

where d is your variable or number

or

 DecimalFormat f = new DecimalFormat("##.00");  // this will helps you to always keeps in two decimal places
 System.out.println(f.format(d)); 
2

You need to use something like NumberFormat with appropriate locale to format

NumberFormat.getCurrencyInstance().format(bigDecimal);
kosa
  • 63,683
  • 12
  • 118
  • 157
  • 3
    This will print the currency symbol, e.g. $, along with the number, which the OP did not mention he wanted. But like the thought of using the currency instance here for readability. – pierus Dec 15 '15 at 15:36
2

BigDecimal.setScale would work.

engineer
  • 21,217
  • 5
  • 29
  • 23
0

The below code may help.

protected String getLocalizedBigDecimalValue(BigDecimal input, Locale locale) {
    final NumberFormat numberFormat = NumberFormat.getNumberInstance(locale);
    numberFormat.setGroupingUsed(true);
    numberFormat.setMaximumFractionDigits(2);
    numberFormat.setMinimumFractionDigits(2);
    return numberFormat.format(input);
}
Narasimha A
  • 249
  • 1
  • 13