Thursday, 17 March 2016

Rounding Bigdecimal values with 4 or 2 Decimal Places.

Rounding Bigdecimal values with 4 or 2 Decimal Places.






BigDecimal bigd = new BigDecimal(0.345673467367);


BigDecimal b1=bigd.setScale(4, BigDecimal.ROUND_UP);

BigDecimal b2=bigd.setScale(4, BigDecimal.ROUND_CEILING);
BigDecimal b3=bigd.setScale(4, BigDecimal.ROUND_DOWN);
BigDecimal b4=bigd.setScale(4, BigDecimal.ROUND_FLOOR);
BigDecimal b5=bigd.setScale(4, BigDecimal.ROUND_HALF_DOWN);
BigDecimal b6=bigd.setScale(4, BigDecimal.ROUND_HALF_EVEN);
BigDecimal b7=bigd.setScale(4, BigDecimal.ROUND_HALF_UP);

BigDecimal b8=bigd.setScale(2, BigDecimal.ROUND_UP);
BigDecimal b9=bigd.setScale(2, BigDecimal.ROUND_CEILING);
BigDecimal b10=bigd.setScale(2, BigDecimal.ROUND_DOWN);
BigDecimal b11=bigd.setScale(2, BigDecimal.ROUND_FLOOR);
BigDecimal b12=bigd.setScale(2, BigDecimal.ROUND_HALF_DOWN);
BigDecimal b13=bigd.setScale(2, BigDecimal.ROUND_HALF_EVEN);
BigDecimal b14=bigd.setScale(2, BigDecimal.ROUND_HALF_UP);

System.out.println("b1: "+b1);
System.out.println("b2: "+b2);
System.out.println("b3: "+b3);
System.out.println("b4: "+b4);
System.out.println("b5: "+b5);
System.out.println("b6: "+b6);
System.out.println("b7: "+b7);

System.out.println("b8: "+b8);
System.out.println("b9: "+b9);
System.out.println("b10: "+b10);
System.out.println("b11: "+b11);
System.out.println("b12: "+b12);
System.out.println("b13: "+b13);
System.out.println("b14: "+b14);


Out put:

b1: 0.3457
b2: 0.3457
b3: 0.3456
b4: 0.3456
b5: 0.3457
b6: 0.3457
b7: 0.3457
b8: 0.35
b9: 0.35
b10: 0.34
b11: 0.34
b12: 0.35
b13: 0.35
b14: 0.35

No comments:

Post a Comment