-1

I want to round a number to 4 decimals. I have done that :

roundedNumber =  (double)(Math.round(roundedNumber)*10000)/10000;

It doesn't work with a number such as : 0.20425 . I am getting the following output: 0.0

What should I do to get: 0.2042 ?

bish
  • 3,241
  • 9
  • 46
  • 66
user1260928
  • 3,129
  • 6
  • 57
  • 99

2 Answers2

2

Use DecimalFormat

DecimalFormat decimalFormat = new DecimalFormat("#.####");
double formatedNumber =  Double.valueOf(decimalFormat.format(0.20425));

System.out.println(formatedNumber);
codeaholicguy
  • 1,641
  • 1
  • 11
  • 17
1

You should mutiply before rounding:

roundedNumber = Math.round(roundedNumber*10000)/10000.0;
Tagir Valeev
  • 92,683
  • 18
  • 210
  • 320