Another class that it's quite useful for this purpose, it's the DecimalFormat class from the java.text package:
import java.text.DecimalFormat;The constructor of this class takes as parameter a string which represents a regex (regular expression) that will be used for formatting the number. The special symbols which can be used are:
- 0 - a digit will be added to the string. If a digit does not exist in that place, 0 will be added
- # - if a digit exists in that place, it will be added to the string
- . - signifies the decimal separator
- , - signifies the group separator
- E - the string will format the number according to the e notation (scientific notation
- % - if placed at the end of the regex, the result will be multiplied with 100 and '%' character will be appended to the end of the string
- /uxxxx - the character with the Unicode xxxx will be appended to the formatted string
- 'x' - used for special characters
double nr1 = 3892.2344, nr2 = 24.832; DecimalFormat format1 = new DecimalFormat("00.00"); System.out.println(format1.format(nr1) + " " + format1.format(nr2)); //Output: 3892.23 24.83 DecimalFormat format2 = new DecimalFormat("#000.00##"); System.out.println(format2.format(nr2) + " " + format2.format(nr2)); //Output: 024.832 024.832 DecimalFormat format3 = new DecimalFormat("$0.00"); System.out.println(format3.format(nr1) + " " + format3.format(nr2)); //Output: $3892.23 $24.83 DecimalFormat format4 = new DecimalFormat("00.E0"); System.out.println(format4.format(nr1) + " " + format4.format(nr2)); //Output: 39.E2 25.E0 DecimalFormat format5 = new DecimalFormat("00.00%"); System.out.println(format5.format(nr1) + " " + format5.format(nr2)); //Output: 389223.44% 2483.20% DecimalFormat format6 = new DecimalFormat("0,0"); System.out.println(format6.format(nr1) + " " + format6.format(nr2)); //Output: 3,8,9,2 2,5 DecimalFormat format7 = new DecimalFormat("\u0045 00.00"); System.out.println(format7.format(nr1) + " " + format7.format(nr2)); //Output: E 3892.23 E 24.83 DecimalFormat format8 = new DecimalFormat("'E' 00.00"); System.out.println(format8.format(nr1) + " " + format8.format(nr2)); //Output: E 3892.23 E 24.83One must note that unlike the NumberFormat, the DecimalFormat will not truncate the integer part and will output a number as is, even if the pattern specifies fewer digits.
Also, you can add any character you want to the format (like I added the dollar sign for format3), as long is not one of the special characters listed above. If you really need to add a character that defined as special, you can specify its Unicode(see format7) or you can put it in inside single quotes ' ' (see format8).
Also one must note that DecimalFormat is a subclass of NumberFormat, so the Locale-related methods are still available.
If your pattern does not respect the defined rules (let's say is like "E.00.00,##" - which is wrong for so many reasons), an IllegalArgumentException will be thrown.
No comments:
Post a Comment
Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!