I am making a java program in which i have to round off the double digit to seven place but I don't know how to do it.
like 6.6666667e-10 to 0.0000001
Asked
Active
Viewed 524 times
0
Nicolas Filotto
- 41,524
- 11
- 90
- 115
Nicky Manali
- 386
- 3
- 22
-
DecimalFormat df = new DecimalFormat("#.#######"); df.setRoundingMode(RoundingMode.CEILING); – i-bob Oct 09 '16 at 11:43
-
@i-bob it's giving 0 for above answer but i need 0.0000001 – Nicky Manali Oct 09 '16 at 11:58
1 Answers
1
The reason it return 0 is because the number example is too small. it can't be rounded to 7 digits after decimal point because the first nonzero digit in it's full decimal representation is after the 7th digit.
However, for slightly bigger numbers, this code should do the trick:
double a = 6.66666667E-10;
DecimalFormat df = new DecimalFormat("#.#######");
df.setRoundingMode(RoundingMode.CEILING);
System.out.println(df.format(a));
ItamarG3
- 3,974
- 6
- 29
- 42
-
-
Like I said, the number in the example is too small. Try a bigger number or try with more than 7 digits. – ItamarG3 Oct 09 '16 at 12:26