Java number format Example
There are many situations that we want to have a specific format in some numbers of our java applications. Depending on the format of the number, this may be done with different ways. Generally for a more well-written code and a more dynamic way, it is recommended the use of DecimalFormat
class. This class give us the manner of formatting decimal numbers into specific strings and to control the leading zeros or prefixes, the group separators etc.
In this example we are going to show with different ways how to format a double number in order to have specific digits in decimal part of this number. More specifically, we will show the utilize of DecimalFormat
and how we can use it in order to achieve a desirable format for a number.
1. Explanation of Classes
Before the display of the code, we are going to explain some basic things about the classes we are going to use.
1.1. BigDecimal
The BigDecimal
class gives us control over rounding behavior and also we can define the number’s scale. The scale indicates the number of digits in the decimal part of the number and its default value is zero. We can apply the desirable scale as well as the rounding mode , via setScale
method.
1.2. DecimalFormat
To format a number we should create a DecimalFormat
and set a String pattern, which determines how the number looks like. When we define zero (0) in the pattern, we indicate the display of one digit (if there isn’t any digit, 0 is appeared in this position). The #
sign declares a digit or nothing if no digit exists. Also, we can use decimal point (.) to separate the decimal part or comma (,) in order to group numbers. Finally, ‘ sign is used to quote a prefix string in the number and %
multiplies the number with 100 in order to take the percentage. It is worth to mention that we described only the symbols that we use in this example, but you can find more here.
2. Example of number format
Create a java class with name NumberFormaClass
and paste the following code.
NumberFormaClass.java:
package com.javacodegeeks.core.numformat; import java.math.BigDecimal; import java.text.DecimalFormat; public class NumberFormaClass { public static void main(String[] args) { double decimal = 7.27467; System.out.println("The test number: "+decimal); int decimalPlaces = 3; // the scale for the decimal // use of BigDecimal class BigDecimal bd = new BigDecimal(decimal); // set the scale and round up if >= 0.5 bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP); double bigDecimal = bd.doubleValue(); System.out.println("BigDecimal rounded in 3rd decimal: "+bigDecimal); // use of DecimalFormat DecimalFormat decFormat = new DecimalFormat("#.00"); double formatDecimal = new Double(decFormat.format(decimal)).doubleValue(); System.out.println("DecimalFormat rounded in 2nd decimal: "+formatDecimal); System.out.println("--------------------------------------"); DecimalFormat numFormat; String number; // 2 digits before decimal point and then 2 digits (rounded) numFormat = new DecimalFormat("000.##"); number = numFormat.format(-15.567); System.out.println("1. DecimalFormat with .: " + number); // string '$' in front of the number numFormat = new DecimalFormat("'$'00.####"); number = numFormat.format(15.567); System.out.println("2. DecimalFormat with '$': " + number); // use of , to group numbers numFormat = new DecimalFormat("#,###,###"); number = numFormat.format(1556789); System.out.println("3. DecimalFormat with ,: " + number); // use of % for percentage numFormat = new DecimalFormat("%"); number = numFormat.format(0.15); System.out.println("4. DecimalFormat with percentage: " + number); // 2 digits before decimal point and 2 digits after numFormat = new DecimalFormat("00.00"); number = numFormat.format(-15.567); System.out.println("5. DecimalFormat with 4 digits: " + number); // left part of decimal number numFormat = new DecimalFormat("##"); number = numFormat.format(156.567); System.out.println("6. DecimalFormat with no decimal part: " + number); // 5 or less digits in the decimal part numFormat = new DecimalFormat(".#####"); number = numFormat.format(1890.567); System.out.println("7. DecimalFormat with 5 or less digits (in decimal part): " + number); // string 'JCG' in front of the number numFormat = new DecimalFormat("'JCG'000.#"); number = numFormat.format(15.567); System.out.println("8. DecimalFormat with 'JCG': " + number); } }
Now lets explain the code above. For the BigDemical
instance we use setScale()
as we mentioned before. So, we set the scale as 3 and we define the rounding mode as ROUND_HALF_UP
in order to round into the “nearest neighbor” (round up for greater than or equal to 0.5). Also, doubleValue()
is called in order to convert the instance to a double.
In this tutorial we have more examples of DecimalFormat
instances because, as we said before, is the most dynamic way for the number format.
#.00
: when this pattern is used, we receive double numbers with 2 digits in the decimal part (or zeros if digits don’t exist).000.##
: this pattern indicates three digits at the left part of the double number and 2 digits at the right part (or nothing if the digits don’t exist).'$'00.####
: the quotes (‘ ‘) define the prefix string, where in this situation is $. The 0 and # declare the number format as we mentioned above.#,###,###
: commas is a placeholder for grouping separator.%
: multiplies the number with 100 in order to show the percentage.
You can also see more number formats in the code above.
Below, is the output of the execution. Notice that in some DecimalFormat
instances there is a rounding too.
Output:
The test number: 7.27467
BigDecimal rounded in 3rd decimal: 7.275
DecimalFormat rounded in 2nd decimal: 7.27
--------------------------------------
1. DecimalFormat with .: -015.57
2. DecimalFormat with '$': $15.567
3. DecimalFormat with ,: 1,556,789
4. DecimalFormat with percentage: %15
5. DecimalFormat with 4 digits: -15.57
6. DecimalFormat with no decimal part: 157
7. DecimalFormat with 5 or less digits (in decimal part): 1890.567
8. DecimalFormat with 'JCG': JCG015.6
Download the source file
This was a tutorial about number format in Java. Download the source code of this example: NumberFormatExample.zip