Java
download.java.net › java › early_access › panama › docs › api › java.base › java › text › NumberFormat.html
NumberFormat (Java SE 19 & JDK 19 [build 1])
declaration: module: java.base, package: java.text, class: NumberFormat
Oracle
docs.oracle.com › javase › 8 › docs › api › java › text › NumberFormat.html
NumberFormat (Java Platform SE 8 )
1 week ago - Java™ Platform Standard Ed. 8 ... NumberFormat is the abstract base class for all number formats. This class provides the interface for formatting and parsing numbers.
Videos
Formatting Money & Percents in Java using the ...
21:39
Number Formatting in Java - YouTube
01:59
Java: formatting numbers with NumberFormat - YouTube
04:28
Java DecimalFormat - Java Number Format - YouTube
08:29
Java Number Format Tutorial - YouTube
01:51
Java Currency Number Format | How to Currency Format in Java | ...
Baeldung
baeldung.com › home › java › java numbers › number formatting in java
Number Formatting in Java | Baeldung
August 9, 2024 - DecimalFormat is one of the most popular ways to format a decimal number in Java. Similar to previous examples, we’ll write a helper method: public static double withDecimalFormatLocal(double value) { DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(Locale.getDefault()); return new Double(df.format(value)); }
ZetCode
zetcode.com › java › numberformat
Java NumberFormat - formatting numbers and currencies in Java
July 4, 2024 - Java NumberFormat tutorial shows how to format and parse numbers and currencies in Java. We set the number of fractional digits, round numbers, and group digits.
Oracle
docs.oracle.com › javase › 7 › docs › api › java › text › NumberFormat.html
NumberFormat (Java Platform SE 7 )
Java™ Platform Standard Ed. 7 ... NumberFormat is the abstract base class for all number formats. This class provides the interface for formatting and parsing numbers.
Java
download.java.net › java › early_access › jdk23 › docs › api › java.base › java › text › NumberFormat.html
NumberFormat (Java SE 23 & JDK 23 [build 22])
declaration: module: java.base, package: java.text, class: NumberFormat
Java
download.java.net › java › early_access › valhalla › docs › api › java.base › java › text › NumberFormat.html
NumberFormat (Java SE 23 & JDK 23 [build 1])
declaration: module: java.base, package: java.text, class: NumberFormat
Java
download.java.net › java › early_access › valhalla › docs › api › java.base › java › text › NumberFormat.Style.html
NumberFormat.Style (Java SE 23 & JDK 23 [build 1])
declaration: module: java.base, package: java.text, class: NumberFormat, enum: Style
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › text › NumberFormat.html
NumberFormat (Java SE 11 & JDK 11 )
January 20, 2026 - You can also control the display of numbers with such methods as setMinimumFractionDigits. If you want even more control over the format or parsing, or want to give your users more control, you can try casting the NumberFormat you get from the factory methods to a DecimalFormat.
Top answer 1 of 4
3
Use DecimalFormat()
DecimalFormat df2 = new DecimalFormat( "#,###,###,##0.00" );
double d = 100.2397;
double d1 = new Double(df2.format(d)).doubleValue();
Output - d1 will be 100.24
2 of 4
3
You need to use a number formatter as shown in the example below:
NumberFormat myFormatter = new DecimalFormat("#.###");
double myNumber = 1234.5632;
String formattedNumber = myFormatter.format(myNumber);
and formattedNumber will be equal to "1234.563"
Tabnine
tabnine.com › home page › code › java › java.text.numberformat
Java Examples & Tutorials of NumberFormat.format (java.text) | Tabnine
@Override public String toPrintableString() { NumberFormat nf = NumberFormat.getInstance(); return "triangle triplet count: " + nf.format(triangleTripletCount) + "; rectangle triplet count: " + nf.format(rectangleTripletCount) + "; maximum triangle triplets: " + nf.format(maximumTriangleTriplets) + "; maximum rectangle triplets: " + nf.format(maximumRectangleTriplets); }
Top answer 1 of 9
125
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.
2 of 9
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);
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › text › NumberFormat.html
NumberFormat (Java SE 17 & JDK 17)
January 20, 2026 - You can also control the display of numbers with such methods as setMinimumFractionDigits. If you want even more control over the format or parsing, or want to give your users more control, you can try casting the NumberFormat you get from the factory methods to a DecimalFormat or CompactNumberFormat depending on the factory method used.
Classpath
developer.classpath.org › doc › java › text › NumberFormat-source.html
Source for java.text.NumberFormat (GNU Classpath 0.95 Documentation)
*/ 38: 39: 40: package java.text; 41: 42: import gnu.java.locale.LocaleHelper; 43: 44: import java.io.IOException; 45: import java.io.InvalidObjectException; 46: import java.io.ObjectInputStream; 47: import java.io.ObjectOutputStream; 48: 49: import java.text.spi.NumberFormatProvider; 50: 51: import java.util.Currency; 52: import java.util.Locale; 53: import java.util.MissingResourceException; 54: import java.util.ResourceBundle; 55: import java.util.ServiceLoader; 56: 57: /** 58: * This is the abstract superclass of all classes which format and 59: * parse numeric values such as decimal numbers, integers, currency values, 60: * and percentages.
Mpg
resources.mpi-inf.mpg.de › d5 › teaching › ss05 › is05 › javadoc › java › text › NumberFormat.html
java.text Class NumberFormat
Get the set of Locales for which NumberFormats are installed · Returns: available locales · public int hashCode() Overrides hashCode · Overrides: hashCode in class Object · Returns: a hash code value for this object. See Also: Object.equals(java.lang.Object), Hashtable ·
Java
download.java.net › java › early_access › loom › docs › api › java.base › java › text › class-use › NumberFormat.html
Uses of Class java.text.NumberFormat (Java SE 25 & JDK 25 [build 1])
use: module: java.base, package: java.text, class: NumberFormat
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.text.numberformat.format
NumberFormat.Format Method (Java.Text) | Microsoft Learn
[<Android.Runtime.Register("format", "(DLjava/lang/StringBuffer;Ljava/text/FieldPosition;)Ljava/lang/StringBuffer;", "GetFormat_DLjava_lang_StringBuffer_Ljava_text_FieldPosition_Handler")>] override this.Format : double * Java.Lang.StringBuffer * Java.Text.FieldPosition -> Java.Lang.StringBuffer ... keeps track on the position of the field within the returned string. For example, for formatting a number 1234567.89 in Locale.US locale, if the given fieldPosition is NumberFormat#INTEGER_FIELD, the begin index and end index of fieldPosition will be set to 0 and 9, respectively for the output string 1,234,567.89.