Format a value, such as 123456789.123, and hope to display it as "$123,456,789.123". To complete the requirements, you can use the java.text.NumberFormat class to format it.
The NumberFormat class provides encapsulation of logarithmic formats. In JDK, this function is generally completed using the subclass of NumberFormt - java.text.DecimalFormat. The most common constructors of this class are:
public DecimalFormat(String pattern)
Where, the parameter pattern represents the passed format string
Code:
The code copy is 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 various parts of the value. #Denote Arabic numerals
In the format string, except for the representative part, other parts such as $ appear as it is