2

how to round 0.055 value to 0.06 in java. I have tried DecimalFormat but it returns 0.05

Prashant Kumar
  • 308
  • 2
  • 3
  • 19

5 Answers5

2

A kind of trivial way

double d = 0.055;       
d= Math.round(d* 100);
d= d/100;
System.out.println(d);
Loki
  • 4,065
  • 4
  • 29
  • 51
1

This is good for you ;

new BigDecimal(String.valueOf(0.055)).setScale(2, BigDecimal.ROUND_HALF_UP)
newuserua_ext
  • 577
  • 6
  • 18
0

Try below code.

DecimalFormat df=new DecimalFormat("0.00");
String formate = df.format(0.055);
double finalValue = (Double)df.parse(formate) ;
System.out.println(finalValue);

Output:

0.06

Reference Link : Double value to round up in Java

Community
  • 1
  • 1
Pratik
  • 944
  • 4
  • 20
0
public static void main(String[] args) {
     double d = 0.055;
     DecimalFormat f = new DecimalFormat("##.00");
     System.out.println(f.format(d));
}

Output:

.06
Touchstone
  • 5,575
  • 7
  • 41
  • 48
0
round(0.055, 2); // returns 0.06

public static double round(double value, int places) {
  if (places < 0) throw new IllegalArgumentException();

  BigDecimal bd = new BigDecimal(value);
  bd = bd.setScale(places, RoundingMode.HALF_UP);
  return bd.doubleValue();
}
seenukarthi
  • 8,241
  • 10
  • 47
  • 68
Rahul Thachilath
  • 363
  • 3
  • 16