-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,381
  • 9
  • 48
  • 69
user1260928
  • 3,269
  • 9
  • 59
  • 105

2 Answers2

2

Use DecimalFormat

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

System.out.println(formatedNumber);
codeaholicguy
  • 1,671
  • 1
  • 11
  • 18
1

You should mutiply before rounding:

roundedNumber = Math.round(roundedNumber*10000)/10000.0;
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334