Java DecimalFormat
last modified July 8, 2023
Java DecimalFormat tutorial shows how to format numbers in Java.
DecimalFormat
class is used to format numbers. It is a concrete
subclass of the NumberFormat
class.
NumberFormat
is used to format numbers for the most common cases.
DecimalFormat
gives more options; it allows us to define our
formatting options.
DecimalFormat
is located in the java.text
package.
Java DecimalFormat simple example
The following is a simple example with DecimalFormat
.
The 0
format specifier stands for a digit. The #
format specifier stands for a digit, where zero shows as absent. The
.
specifier is for a decimal separator.
package com.zetcode; import java.text.DecimalFormat; public class DecimalFormatEx { public static void main(String[] args) { double[] vals = new double[] {0.31, 5.60, 6.7, 5}; var pattern1 = "#.##"; var pattern2 = "#.00"; var df1 = new DecimalFormat(pattern1); var df2 = new DecimalFormat(pattern2); for (var val : vals) { System.out.printf("%4s - %4s %n", df1.format(val), df2.format(val)); } } }
The program prints four double values using two format specifiers.
0.31 - .31 5.6 - 5.60 6.7 - 6.70 5 - 5.00
Java DecimalFormat applyPattern
The applyPattern
method applies the given pattern to an existing
format.
package com.zetcode; import java.text.DecimalFormat; public class DecimalFormatEx { public static void main(String[] args) { double n = 1240.30; var df = new DecimalFormat("#.##"); System.out.println(df.format(n)); df.applyPattern("#.00"); System.out.println(df.format(n)); } }
The program formats a double value in two formats.
var df = new DecimalFormat("#.##");
We create a new instance of the DecimalFormat
. We pass it a
non-localized pattern string. The pattern defines a format for a decimal value
with a dot followed by two decimal places.
df.applyPattern("#.00");
We specify a new pattern with applyPattern
. This pattern adds zeros
to decimal places, if they are empty.
1240.3 1240.30
Java DecimalFormat grouping
The ,
format character is used for grouping of digits.
package com.zetcode; import java.text.DecimalFormat; public class DecimalFormatEx { public static void main(String[] args) { double n = 2_125_405.30; String pattern = "###,###.00"; var df = new DecimalFormat(pattern); System.out.println(df.format(n)); } }
The program prints a double value, whose digits are grouped.
2,125,405.30
Java DecimalFormat percentage
With the %
format character, we define percentages.
package com.zetcode; import java.text.DecimalFormat; public class DecimalFormatEx { public static void main(String[] args) { double n = 0.34; var pattern = "#.##%"; var df = new DecimalFormat(pattern); System.out.println(df.format(n)); } }
The program prints a double value as a percentage.
34%
DecimalFormat in string literal
We can put the formatted value in a string literal.
package com.zetcode; import java.text.DecimalFormat; public class DecimalFormatEx { public static void main(String[] args) { double n = 7.34; var pattern = "The #.## number"; var df = new DecimalFormat(pattern); System.out.println(df.format(n)); } }
The program prints the formatted double value inside a string.
The 7.34 number
Java localized DecimalFormat
The next example localizes the number formats.
package com.zetcode; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.Locale; public class DecimalFormatEx { public static void main(String[] args) { double n = 127_540.30; var skLoc = new Locale("sk", "SK"); var usLoc = new Locale("us", "US"); var deLoc = new Locale("de", "DE"); var pattern = "###,###.##"; NumberFormat nf = NumberFormat.getNumberInstance(skLoc); DecimalFormat df = (DecimalFormat)nf; df.applyPattern(pattern); System.out.println(df.format(n)); NumberFormat nf2 = NumberFormat.getNumberInstance(usLoc); DecimalFormat df2 = (DecimalFormat)nf2; df2.applyPattern(pattern); System.out.println(df2.format(n)); NumberFormat nf3 = NumberFormat.getNumberInstance(deLoc); DecimalFormat df3 = (DecimalFormat)nf3; df3.applyPattern(pattern); System.out.println(df3.format(n)); } }
In the program, we print a value in three different locales. The grouping and the decimal separators chosen according to the given language cultures.
127 540,3 127,540.3 127.540,3
In this article we have shown how to format numbers in Java with
DecimalFormat
.
Author
List all Java tutorials.