Issue
I have a program like this ,
BigDecimal bd = new BigDecimal(23.086);
BigDecimal bd1= new BigDecimal(0.000);
bd = bd.setScale(2, RoundingMode.HALF_UP).stripTrailingZeros();
bd1 = bd1.setScale(2, RoundingMode.HALF_UP).stripTrailingZeros();
System.out.println("bd value::"+ bd);
System.out.println("bd1 value::"+ bd1);
I get the following output: 23.09
for bd
and 0.00 for bd1
, but I want bd1
as 0
not as 0.00
. Am I applying the methods correctly?
Solution
BigDecimal bd = new BigDecimal(23.086);
BigDecimal bd1 = new BigDecimal(0.000);
bd = bd.setScale(2, RoundingMode.HALF_UP).stripTrailingZeros();
bd1 = bd1.setScale(2, RoundingMode.HALF_UP).stripTrailingZeros();
System.out.println("bd value::"+ bd); System.out.println("bd1 value::"+ bd1);
System.out.println("new way:" + bd1.intValueExact());
//OUTPUT bd value::23.09
bd1 value::0.00
new way:0
Answered By - Marcia Ong
Answer Checked By - Mary Flores (JavaFixing Volunteer)