Java formats numbers as currency string instance code _java

Source: Internet
Author: User
Tags locale

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.