-1

I want to round of a double value to the immediate next number.

Tried working with Math.ceil and Math.round but it didnt help.

Below is the condition and code:

Double totalAsset = Util.convertToDouble(dataObject) * calculus;

// 62072560. * 06433535204605706 = 399345.99999999994

System.out.println(totalAsset.longValue());

But i get 399345 whereas it should be 399346.

ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
sTg
  • 4,313
  • 16
  • 68
  • 115
  • Define "the rest" please. – Tim Biegeleisen Jul 31 '17 at 11:15
  • rest means when we have the decimal starting from 5. it should round off – sTg Jul 31 '17 at 11:17
  • 2
    No, that still might not be clear to everyone. Please show a series of numbers and how you want them to round. – Tim Biegeleisen Jul 31 '17 at 11:17
  • For Eg: A number being 399345.5, 399345.6,399345.7,399345.8,399345.9 should round off to immediate next number. – sTg Jul 31 '17 at 11:21
  • Do you mean, for example, that `1.899999` should "round" to `1` and `1.9` should round to `2`? In other words, if the fractional part is less than `0.9` then round down, otherwise round up? If so, try `int(x+0.1)` for positive numbers. Due to the non-exactness of `0.1` in floating point this will rarely be off but it would work almost always. – Rory Daulton Jul 31 '17 at 11:21
  • 1.899999 should round of to 1.9 – sTg Jul 31 '17 at 11:22
  • So you want to round normally to the first decimal? – Madmenyo Jul 31 '17 at 11:23
  • Yes @Madmenyo rite – sTg Jul 31 '17 at 11:25
  • 3
    That's what `Math.round` does. In what way did it fail to work for you? – Dawood ibn Kareem Jul 31 '17 at 11:29
  • Math.round was returning 399346 even for 399345.4 – sTg Jul 31 '17 at 11:31
  • `06433535204605706` is integer – Ali Faris Jul 31 '17 at 11:36
  • refer to [Math.round(double)](https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#round(double)). _Returns: the value of the argument rounded to the nearest long value._ – Ravinder Reddy Jul 31 '17 at 11:36
  • 1
    @sTg Really? I just tested `System.out.println(Math.round(399345.4));` and it printed `399345`. – Dawood ibn Kareem Jul 31 '17 at 11:36
  • @sTg Try to describe what you want/need better to avoid clutter and answers your not looking for. Test things in the simplest way possible first before you make incorrect statements. The 3th comment by Tim is asking for a list of numbers with values you want them to round too, that would have made things easy. Now this whole page is a giant mess. – Madmenyo Jul 31 '17 at 11:48

4 Answers4

1

Do you mean something like this? Each println() statement prints 399346.

public class Test {
    public static void main(final String[] args) {
        final long l0 = (long) Math.ceil(399345.1); //Becomes 399346
        final long l1 = (long) Math.ceil(399345.5); //Becomes 399346
        final long l2 = (long) Math.ceil(399345.6); //Becomes 399346
        final long l3 = (long) Math.ceil(399345.7); //Becomes 399346
        final long l4 = (long) Math.ceil(399345.8); //Becomes 399346
        final long l5 = (long) Math.ceil(399345.9); //Becomes 399346

        System.out.println(l0); //Prints 399346
        System.out.println(l1); //Prints 399346
        System.out.println(l2); //Prints 399346
        System.out.println(l3); //Prints 399346
        System.out.println(l4); //Prints 399346
        System.out.println(l5); //Prints 399346
    }
}
Tmr
  • 268
  • 1
  • 7
1

To print, and thus not lose any precision you can use DecimalFormat.

DecimalFormat df = new DecimalFormat("#.#");
double d = 1.899999;
System.out.println(df.format(d));

Otherwise, precise rounding of a double has some strings attached to it. I advise reading the following answer which I ripped this method from:

public static double round(double value, int places) {
  if (places < 0) throw new IllegalArgumentException();

  BigDecimal bd = new BigDecimal(value);
  bd = bd.setScale(places, RoundingMode.HALF_UP);
  return bd.doubleValue();
}

If you do want to ceil your numbers, which is still unclear to me you can change the RoundingMode to RoundingMode.CEILING. Read more about RoundingMode here.

Madmenyo
  • 8,389
  • 7
  • 52
  • 99
0

Could adding an If-statement help?

Substract the real result (1234,56) with the shortened one (1234) and check if the result is >= 0,5 to round up or < 0,5 to round down?

Nigel-Lee
  • 145
  • 1
  • 13
0

If you don't mind the overhead, than BigDecimal comes to your rescue:

final BigDecimal a = new BigDecimal("1.899999");
final BigDecimal b = new BigDecimal("1.911111");
System.out.println(a.setScale(1, RoundingMode.UP)); // prints 1.9
System.out.println(b.setScale(1, RoundingMode.UP)); // prints 2.0
D. Kovács
  • 1,232
  • 13
  • 25