I have this number - 68.65121847597993 and I want to round it like this - 68.7. How can I do this using Java?
7 Answers
I would do it as in nearly every other language:
double input = -68.65121847597993;
double rounded = Math.floor(input * 10.0 + 0.5)/10.0;
With a BigDecimal you have more options like a rounding mode and setScale if you need that.
And there are third party libraries like decimal4j or Apache Commons Math - though I didn't test them.
- 1,338
- 7
- 16
-
@LinFelix did you try my code? I admit that I overlooked the negative sign, so I updated my answer, but it with the negative sign it returns `-68.7`. This is due to the nature of `Math.floor()`, which does not truncate, but rounds down to the next smaller whole number, which is -687.0 if the input is -686.0121847597993 as in this example. – cyberbrain May 06 '22 at 07:47
In order to represent a number with that many decimal points, I presume you are using BigDecimal rather than double, which can only accurately represent approximately 15 significant digits.
Starting from a BigDecimal, which you can initialize with a string, a double, or anything else you have, you can call BigDecimal::setScale to get the value you want.
For example:
import java.math.*;
public class HelloWorld {
public static void main(String []args){
BigDecimal b = new BigDecimal("68.65121847597993");
BigDecimal rounded = b.setScale(2, RoundingMode.HALF_UP);
System.out.println(rounded); // 68.65
}
}
- 11
- 2
-
-68.65121847597993 (and its positive variant 68.65121847597993) fit nicely into a `double`. – cyberbrain May 06 '22 at 07:48
-
It's on the knife's edge, getting into dangerous territory. E.g. ``` double a = 68.65121847597993; double b = -68.651218475979931; System.out.println(a == -b); // true `` – Vijay Nayar May 06 '22 at 13:12
-
comparing `float` and `double` with `==` is always "on the knife's edge" ;-) but I get your point. I suppose that the OP copied that number from code that used actually a double value before, but maybe I'm wrong - so your answer is really helpful! – cyberbrain May 06 '22 at 19:28
You should try this
DecimalFormat df = new DecimalFormat("###.###");
Here's a Baeldung article about it: https://www.baeldung.com/java-round-decimal-number
Hope it helps!
- 1
- 2
You cannot really do that because floats and doubles do not work like that.
However, this could solve your problem:
round((-68.65121847597993)*10)/10f;
- 550
- 1
- 11
- 21
-
Your example doesn't fully work: you are mixing `double`, `int` and `float` to a bad: the division by `10f` makes the result `-68.69999694824219`, if the result is assigned to a `double` again. Only assigning it to a `float` yields to the correct result. Always take care of automatic casts and mixing numeric types. – cyberbrain May 06 '22 at 07:55
-
If your value is a double, this method takes the lowest time on compiling:
Math.round(num * 10d) / 10d
Number of 0's = number of decimals
- 321
- 1
- 11
Try this
BigDecimal bd = new BigDecimal(Double.toString(number));
bd = bd.setScale(decimalPlaces, RoundingMode.HALF_UP);
return bd.doubleValue();
- 13
- 1
- 5
Rather than change the number (so its current precision can be still used in subsequent calculations), you can control the display as follows.
double v = 68.65121847597993;
System.out.printf("%.1f%n", v);
prints
68.7
- 30,162
- 4
- 20
- 36