Decimal format Java Example
In this post, you will learn about the Decimal format with examples using the Java programming language.
Java DecimalFormat class
DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. The java.text.DecimalFormat class is used for formatting numbers as per customized format and as per locale. A DecimalFormat comprises a pattern and a set of symbols. A DecimalFormat pattern contains a positive and negative subpattern, for example, "#,##0.00;(#,##0.00)".
Syntax-DecimalFormat decimalFormat = new DecimalFormat(pattern);
Number Format Pattern Syntax
In the given Java program, we have formatted numbers based on a given pattern.
These are the some mostly used patterns-
Symbol | Location | Localized | Description |
0 | Number | Yes | prints a digit if provided, 0 otherwise |
# | Number | Yes | Prints a digit if provided, nothing otherwise |
. | Number | Yes | Indicate where to put the decimal separator |
- | Number | Yes | Minus sign |
, | Number | Yes | Indicate where to put the grouping separator |
; | Subpattern boundary | Yes | Separates positive and negative subpatterns |
Java Decimal Format Examples
To format a number, we have to define a pattern, which is a sequence of special characters. Here, we have used the format() method of the DecimalFormat instance for number formatting. When the specified pattern gets applied to the number, we get a formatted decimal number as output.
import java.text.DecimalFormat;
public class DecimalNumberFormat {
public static void main(String[] args) {
String pattern = "####,####.##";
double number = 342344.543;
DecimalFormat numberFormat = new DecimalFormat(pattern);
System.out.println(number);
System.out.println(numberFormat.format(number));
}
}
Output of the above code:
342344.543
34,2344.54
Java DecimalFormat For a Specific Locale
To create a DecimalFormat for a specific Locale, we have to instantiate the Locale() class, create a NumberFormat and cast it to a DecimalFormat.
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
public class DecimalNumberFormat {
public static void main(String[] args) {
Locale en_locale = new Locale("en", "US");
Locale da_locale = new Locale("da", "DK");
String pattern = "####,####.##";
double number = 342344.543;
DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getNumberInstance(en_locale);
decimalFormat.applyPattern(pattern);
System.out.println(decimalFormat.format(number));
decimalFormat = (DecimalFormat) NumberFormat.getNumberInstance(da_locale);
decimalFormat.applyPattern(pattern);
System.out.println(decimalFormat.format(number));
}
}
Output of the above code:
34,2344.54
34.2344,54
Java DecimalFormat Grouping Digits
We can use the setGroupingSize() method of DecimalFormat to change the default grouping of numbers.
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class DecimalNumberFormat {
public static void main(String[] args) {
double number = 34234452.543;
DecimalFormat decimalFormat = new DecimalFormat();
System.out.println(decimalFormat.format(number));
decimalFormat.setGroupingSize(4);
System.out.println(decimalFormat.format(number));
}
}
Output of the above code:
34,234,452.543
3423,4452.543
Related Articles
Number pattern programs in JavaJava program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number