About 107,000 results
Open links in new tab
  1. How to round a number to n decimal places in Java

    Sep 30, 2008 · I came here just wanting a simple answer on how to round a number. This is a supplemental answer to provide that. How to round a number in Java. The most common case is to use Math.round(). Math.round(3.7) // 4 Numbers are rounded to the nearest whole number. A .5 value is rounded up.

  2. math - Java Round up Any Number - Stack Overflow

    I can't seem to find the answer I'm looking for regarding a simple question: how do I round up any number to the nearest int? For example, whenever the number is 0.2, 0.7, 0.2222, 0.4324, 0.99999 I would want the outcome to be 1. So far I have. int b = (int) Math.ceil(a / 100); It doesn't seem to be doing the job, though.

  3. How to round integer in java - Stack Overflow

    Apr 25, 2011 · Math.round(double) takes the double and then rounds up as an nearest integer. So, 1732 will become 173.2 (input parameter) on processing by Math.round(1732 / 10.0). So the method rounds it like 173.0. Then multiplying it with 10 (Math.round( 1732 / 10.0) * 10) gives the rounded down answer, which is 173.0 will then be casted to int.

  4. java - Using Math.round to round to one decimal place ... - Stack …

    Mar 5, 2014 · The Math.round method returns a long (or an int if you pass in a float), and Java's integer division is the culprit. Cast it back to a double , or use a double literal when dividing by 10 . Either:

  5. Round down a number in java - Stack Overflow

    May 18, 2017 · I would like to know how I can instruct Java to always round down a given number. e.g.: 1.08 rounds to 1 1.56 rounds to 1 1.67 rounds to 1 1.98 rounds to 2 1.89 rounds to 2 I want them rounded to...

  6. java - How do I round to the nearest ten? - Stack Overflow

    Oct 3, 2016 · How to round a number to n decimal places in Java. 66. Rounding up a number to nearest multiple of 5. 13.

  7. round up to 2 decimal places in java? - Stack Overflow

    Jul 28, 2012 · Decimal Format does not round off the last decimal value for any of its functions. The margin for the round off for all the functions is >5 but its supposed to be >4. Eg - 1.235 should round off to 1.24 but the DF formatting rounds it to 1.23 which is …

  8. java - Round to nearest multiple of 5 (either up or down ... - Stack ...

    May 20, 2024 · I need to round a number to nearest multiple of 5 (either up or down). For example, here are the list of numbers and the number next to it that it needs to round up/down to. 12.5 10 62.1 60 68.3 70 74.5 75 80.7 80 Numbers will only be positive.

  9. java - How to round up to the next integer? - Stack Overflow

    Oct 30, 2013 · You must fix your division first. By casting one of the operands to a floating point number, you get a non-integer result as desired. Then you can use Math.ceil to round it up. This code should work: Math.ceil((double)a / b);

  10. How do I round a double to two decimal places in Java?

    You can't 'round a double to [any number of] decimal places', because doubles don't have decimal places. You can convert a double to a base-10 String with N decimal places, because base-10 does have decimal places, but when you convert it back you are back in double-land, with binary fractional places.

Refresh