Numbers can flag currencies, percentages, points, and phone numbers, and in the case of currencies, are defined in different formats in different countries, this example will receive the number entered by the user and then output the currency format in the console using different currency formats.
The idea is as follows: Use the Getcurrencyinstance () method of the NumberFormat class to create different objects with different parameters, using the format () method for the object, which is the number entered by the user.
The code is as follows:
Copy Code code as follows:
Import Java.text.NumberFormat;
Import Java.util.Locale;
Import Java.util.Scanner;
public class FormatNumber
{
public static void Main (string[)
args) {
Scanner
Scan = new Scanner (system.in);//
Create a callout input stream scanner
System.out.println ("Please enter a number:");
Double number
= Scan.nextdouble ();//
Get user input numbers
SYSTEM.OUT.PRINTLN ("This number uses the following constants of the locale class as the structural parameters of the formatted object, and will get a different currency format:");
//
Create a formatted object
NumberFormat
Format = numberformat.getcurrencyinstance (Locale.china);
//
Output formatted currency format
System.out.println ("Locale.china:" +
Format.format (number));
Format
= Numberformat.getcurrencyinstance (locale.us);
System.out.println ("locale.us:" +
Format.format (number));
Format
= Numberformat.getcurrencyinstance (locale.english);
System.out.println ("Locale.english:" +
Format.format (number));
Format
= Numberformat.getcurrencyinstance (Locale.taiwan);
System.out.println ("Locale.taiwan:" +
Format.format (number));
}
}
Effect as shown: