Format a value, such as 123456789.123, that you want to display as "$123,456,789.123". To complete the requirement, you can format it with the Java.text.NumberFormat class
The NumberFormat class provides an encapsulation of numeric formats. In JDK, you typically use the NUMBERFORMT subclass----Java.text.DecimalFormat to complete this function. The most common constructors for this class are:
Public DecimalFormat (String pattern)
Where the parameter pattern represents the passed-in format string
Code:
Copy Code code as follows:
Import Java.text.DecimalFormat;
Import Java.text.NumberFormat;
public class NumberFormat
{
public static void Main (string[] args)
{
NumberFormat NF = new DecimalFormat ("$,###.##");
String teststr = Nf.format (123456789.123);
System.out.println (TESTSTR);
}
}
DecimalFormat Features:
Accept the corresponding format string and format the parts in the numeric values. #表示阿拉伯数字
In a format string, except for the part that has a representative meaning, other parts, such as $, appear as if they were