-2

I have tried following options.

BigDecimal expectedAmount = BigDecimal.valueOf(3000.00);

and

BigDecimal expectedAmount = BigDecimal.valueOf(3000);

and

BigDecimal expectedAmount = new BigDecimal(3000.00);

and

BigDecimal expectedAmount = new BigDecimal(3000);

All of them storing 3000.0 but I wants to store 3000.00 for my junit test. Not sure how to do it.

Naman
  • 27,789
  • 26
  • 218
  • 353
Ashish
  • 14,295
  • 21
  • 82
  • 127

2 Answers2

3

Two more options:

BigDecimal expectedAmount1 = new BigDecimal(3000).setScale(2);
BigDecimal expectedAmount2 = BigDecimal.valueOf(300000L, 2);

The first one will work only for integer amounts. The .setScale(2) then just "adds two zeroes after the decimal point". The second one needs a re-scaled value (e.g. integer cents amount if you want to express dollars), and declares that the decimal point is to be placed two digits left of the end.

Both versions don't need a string representation, which can be quite a performance advantage.

Ralf Kleberhoff
  • 6,990
  • 1
  • 13
  • 7
2

You shall use the constructor accepting the String val as:

BigDecimal expectedAmount = new BigDecimal("3000.00");
Naman
  • 27,789
  • 26
  • 218
  • 353