120

How do I format a number in Java?
What are the "Best Practices"?

Will I need to round a number before I format it?

32.302342342342343 => 32.30

.7323 => 0.73

etc.

Community
  • 1
  • 1
ScArcher2
  • 85,501
  • 44
  • 121
  • 160

9 Answers9

127

From this thread, there are different ways to do this:

double r = 5.1234;
System.out.println(r); // r is 5.1234

int decimalPlaces = 2;
BigDecimal bd = new BigDecimal(r);

// setScale is immutable
bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
r = bd.doubleValue();

System.out.println(r); // r is 5.12

f = (float) (Math.round(n*100.0f)/100.0f);

DecimalFormat df2 = new DecimalFormat( "#,###,###,##0.00" );
double dd = 100.2397;
double dd2dec = new Double(df2.format(dd)).doubleValue();

// The value of dd2dec will be 100.24

The DecimalFormat() seems to be the most dynamic way to do it, and it is also very easy to understand when reading others code.

Miquel
  • 15,405
  • 8
  • 54
  • 87
Espo
  • 41,399
  • 21
  • 132
  • 159
  • Thanks for this explanation. However, Intellij recommends to use `Double.valueOf` instead of `new Double(number).doubleValue()`. The last line of the last example would therefor be `double dd2dec = Double.valueOf(df2.format(dd));` – kumaheiyama Feb 24 '15 at 09:15
  • 1
    The DecimalFormat pattern in this answer is great for US, but wrong in most other locales throughout the world. The grouping character (comma in the US, but space or dot in other locales), the grouping size (threes in US and most locales, but different in India), the decimal character (dot in the US, but comma in other locales). The correct way to get a DecimalFormat instance is: DecimalFormat df = (DecimalFormat)NumberFormat.getNumberInstance(locale) – vonWippersnap Jul 08 '19 at 22:47
74

You and String.format() will be new best friends!

https://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax

 String.format("%.2f", (double)value);
fospathi
  • 537
  • 1
  • 6
  • 7
Jeff Atwood
  • 63,320
  • 48
  • 150
  • 153
  • 30
    This answer would not have got so many upvotes if it had come from anyone else. `String.format` is for formatting strings, not numbers. – Dónal May 15 '13 at 11:06
  • 4
    Actually, String.format is like C's printf. It can format several kinds of data types. – cesarse Jul 02 '13 at 13:36
  • 3
    When you want to convert the rounded value as string, this is one of the way. Other is using `DecimalFormat`. And `DecimalFormat` is slightly faster than `String.format`. Simple `System.currentTimeMillis` diff reveals that. – manikanta Aug 09 '14 at 20:08
  • This for only string value – T8Z Dec 16 '15 at 01:18
  • 1
    This is the best answer. While the number of digits after the decimal point is completely domain-specific, numbers need to be displayed in a locale-aware way. String.format() uses the correct number symbols and decimal character for the default locale. Even better would be to pass the required locale in as the first parameter to format() to make it explicit. – vonWippersnap Jul 08 '19 at 22:51
15

Be aware that classes that descend from NumberFormat (and most other Format descendants) are not synchronized. It is a common (but dangerous) practice to create format objects and store them in static variables in a util class. In practice, it will pretty much always work until it starts experiencing significant load.

Robert J. Walker
  • 10,027
  • 5
  • 46
  • 65
11

Round numbers, yes. This is the main example source.

/*
 * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Sun Microsystems nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */ 


import java.util.*;
import java.text.*;

public class DecimalFormatDemo {

    static public void customFormat(String pattern, double value ) {
        DecimalFormat myFormatter = new DecimalFormat(pattern);
        String output = myFormatter.format(value);
        System.out.println(value + "  " + pattern + "  " + output);
    }

    static public void localizedFormat(String pattern, double value,                                       Locale loc ) {
        NumberFormat nf = NumberFormat.getNumberInstance(loc);
        DecimalFormat df = (DecimalFormat)nf;
        df.applyPattern(pattern);
        String output = df.format(value);
        System.out.println(pattern + "  " + output + "  " + loc.toString());
    }

    static public void main(String[] args) {

        customFormat("###,###.###", 123456.789);
        customFormat("###.##", 123456.789);
        customFormat("000000.000", 123.78);
        customFormat("$###,###.###", 12345.67);
        customFormat("\u00a5###,###.###", 12345.67);

        Locale currentLocale = new Locale("en", "US");

        DecimalFormatSymbols unusualSymbols = new DecimalFormatSymbols(currentLocale);
        unusualSymbols.setDecimalSeparator('|');
        unusualSymbols.setGroupingSeparator('^');
        String strange = "#,##0.###";
        DecimalFormat weirdFormatter = new DecimalFormat(strange, unusualSymbols);
        weirdFormatter.setGroupingSize(4);
        String bizarre = weirdFormatter.format(12345.678);
        System.out.println(bizarre);

        Locale[] locales = {
            new Locale("en", "US"),
            new Locale("de", "DE"),
            new Locale("fr", "FR")
        };

        for (int i = 0; i < locales.length; i++) {
            localizedFormat("###,###.###", 123456.789, locales[i]);
        }
     }
 }
Manoj Sharma
  • 1,467
  • 2
  • 13
  • 20
Vinko Vrsalovic
  • 330,807
  • 53
  • 334
  • 373
8

Try this:

String.format("%.2f", 32.302342342342343);

Simple and efficient.

Kaushik
  • 6,150
  • 5
  • 39
  • 54
Nautilus
  • 180
  • 2
  • 6
6

Use DecimalFormat.

ckpwong
  • 2,129
  • 1
  • 17
  • 17
4

There are two approaches in the standard library. One is to use java.text.DecimalFormat. The other more cryptic methods (String.format, PrintStream.printf, etc) based around java.util.Formatter should keep C programmers happy(ish).

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
3

As Robert has pointed out in his answer: DecimalFormat is neither synchronized nor does the API guarantee thread safety (it might depend on the JVM version/vendor you are using).

Use Spring's Numberformatter instead, which is thread safe.

Stefan Haberl
  • 9,812
  • 7
  • 72
  • 81
3
public static void formatDouble(double myDouble){
 NumberFormat numberFormatter = new DecimalFormat("##.000");
 String result = numberFormatter.format(myDouble);
 System.out.println(result);
}

For instance, if the double value passed into the formatDouble() method is 345.9372, the following will be the result: 345.937 Similarly, if the value .7697 is passed to the method, the following will be the result: .770