About 96,100 results
Open links in new tab
  1. 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 …

  2. How to round a number to n decimal places in Java

    Sep 30, 2008 · However as you can see this uses half-even rounding. That is it will round down if the previous digit is even. What I'd like is this: 0.912385 -> 0.91239 0.912300 -> 0.9123 What is the best way to achieve this in Java?

  3. java - Round a double to 2 decimal places - Stack Overflow

    May 11, 2010 · Item 48: "Avoid float and double if exact answers are required" in Effective Java (2nd ed) by Joshua Bloch What Every Programmer Should Know About Floating-Point Arithmetic If you wanted String formatting instead of (or in addition to) …

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

    If you want the result to two decimal places you can do // assuming you want to round to Infinity. double tip = (long) (amount * percent + 0.5) / 100.0; This result is not precise but Double.toString(double) will correct for this and print one to two decimal places.

  5. I need to round a float to two decimal places in Java

    Jun 17, 2012 · You want to round the String display of a floating point number. You likely don't want to round the number itself. Avoid float, and use double instead for greater precision, and then look at one of the many methods available to give decent String display of a double number including String.format(...), NumberFormat or DecimalFormat.

  6. java - How to round the double value to 2 decimal points ... - Stack ...

    There's no difference in internal representation between 2 and 2.00. You can use Math.round to round a value to the nearest integer - to make that round to 2 decimal places you could multiply by 100, round, and then divide by 100, but you shouldn't expect the result to be exactly 2dps, due to the nature of binary floating point arithmetic.

  7. How can I truncate a double to only two decimal places in Java?

    Oct 12, 2011 · I was looking for a two decimal random number generator whether it is a float or double, i really don't mind. I started with floating number and ended returning double. With the help of the code above from @Rainbow979 with minor changes it worked perfectly. Instead of Math.round, I used Math.floor. thanks to @Rainbow979

  8. Rounding BigDecimal to *always* have two decimal places

    Oct 14, 2016 · I'm trying to round BigDecimal values up, to two decimal places. I'm using BigDecimal rounded = value.round(new MathContext(2, RoundingMode.CEILING)); logger.trace("rounded {} to {}", value, roun...

  9. java - Rounding Bigdecimal values with 2 Decimal Places - Stack …

    I want a function to convert Bigdecimal 10.12 for 10.12345 and 10.13 for 10.12556. But no function is satisfying both conversion in same time.Please help to achieve this. Below is what I tried. With

  10. Double decimal formatting in Java - Stack Overflow

    Jan 4, 2016 · I'm having some problems formatting the decimals of a double. If I have a double value, e.g. 4.0, how do I format the decimals so that it's 4.00 instead?

Refresh