Skip to main content
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

How to round a number to n decimal places in Java

What I would like is a method to convert a double to a string which rounds using the half-up method - i.e. if the decimal to be rounded is 5, it always rounds up to the next number. This is the standard method of rounding most people expect in most situations.

I also would like only significant digits to be displayed - i.e. there should not be any trailing zeroes.

I know one method of doing this is to use the String.format method:

String.format("%.5g%n", 0.912385);

returns:

0.91239

which is great, however it always displays numbers with 5 decimal places even if they are not significant:

String.format("%.5g%n", 0.912300);

returns:

0.91230

Another method is to use the DecimalFormatter:

DecimalFormat df = new DecimalFormat("#.#####");
df.format(0.912385);

returns:

0.91238

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?

Answer*

Cancel
6
  • 10
    This is probably the best solution presented so far. The reason I didn't spot this facility when I first looked at the DecimalFormat class is that it was only introduced in Java 1.6. Unfortunately I'm restricted to using 1.5 but it will be useful to know for the future.
    Alex Spurling
    –  Alex Spurling
    2008-10-01 13:07:09 +00:00
    Commented Oct 1, 2008 at 13:07
  • Doesn't work with exponent decimalformats, say format("0.0E00"). It will in this example round 0.0155 to 0.0E00 instead of 1.6E-02.
    Martin Clemens Bloch
    –  Martin Clemens Bloch
    2014-10-15 23:50:10 +00:00
    Commented Oct 15, 2014 at 23:50
  • 1
    I tried this with: "#.##", rounding HALF_UP. 256.335f -> "256.33" ...(example comes from comments to @asterite's answer).
    bigstones
    –  bigstones
    2015-12-11 11:43:18 +00:00
    Commented Dec 11, 2015 at 11:43
  • 6
    Please be carefull as DecimalFormat depend on your current Local configuration, you may not get a dot as a separator. I personnally prefer Asterite's answer below
    Gomino
    –  Gomino
    2016-03-02 17:01:35 +00:00
    Commented Mar 2, 2016 at 17:01
  • 1
    Also be aware that you should not expect DecimalFormat to be thread-safe. As per Java docs: Decimal formats are generally not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.
    CGK
    –  CGK
    2016-08-10 03:05:50 +00:00
    Commented Aug 10, 2016 at 3:05

Morty Proxy This is a proxified and sanitized view of the page, visit original site.