format - Set precision using decimalFormat error - Java -
i attempting use decimalformat java class first time, , running strange issue. 125.295 round 125.30. think format should automatically include 0, maybe i'm incorrect.
double num = 125.295; decimalformat df = new decimalformat("#.##"); df.setroundingmode(roundingmode.half_up); string str = df.format(num); system.out.println(str); //this yielding 125.3 instead of 125.30
please , thank in advance!
the decimalformat class treats '#' "hide trailing zeroes" or '0' "show zeroes". per api decimalformat:
0 number yes digit # number yes digit, 0 shows absent
so should use decimalformat("#.00") instead of decimalformat("#.##") if want show trailing zeroes.
Comments
Post a Comment