-2

I have this BigDecimal number - 68.65121847597993 and I want to round it like this - 68.7, so only decimical part of number would be rounded up, but not the whole number.

I tried to use RoundingMode.HALF_UP but it rounds up the whole number as well. Is there a way to round up only decimical part of number using java?

Artur May
  • 30
  • 5
  • Do you want to actually modify the number or simply provided a rounded representation of the number? – WJS May 05 '22 at 14:37

7 Answers7

1

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.

cyberbrain
  • 3,433
  • 1
  • 12
  • 22
  • @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
1

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
  }
}
  • -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
0

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!

0

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;

also see: Java float 123.129456 to 123.12 without rounding

LinFelix
  • 1,026
  • 1
  • 13
  • 23
  • 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
  • Yes, it is not a great solution I must admit – LinFelix May 06 '22 at 08:31
0

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

Matei Piele
  • 572
  • 2
  • 15
0

Try this

BigDecimal bd = new BigDecimal(Double.toString(number));
bd = bd.setScale(decimalPlaces, RoundingMode.HALF_UP);
return bd.doubleValue();
Alecasa
  • 40
  • 1
  • 8
0

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
WJS
  • 36,363
  • 4
  • 24
  • 39