Example of using Format to create a formatted object

Source: Internet
Author: User

1. format the number as a currency string:

Import java. text. numberFormat; import java. util. locale; import java. util. secret; public class FormatNumber {public static void main (String [] args) {shortscan = new role (System. in); // create the annotation input Flow Scanner System. out. println ("enter a number:"); double number = scan. nextDouble (); // obtain the user input number System. out. println ("this number uses the following constant of the Locale class as the constructor of the formatting object to obtain different currency formats:"); // create the formatting object NumberFormat format = NumberFormat. getCurrencyInstance (Locale. CHINA); // format the output 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 ));}}


2. format the date

Package com. lixiyu; import java. util. date; public class Year {public static void main (String [] args) {Date date = new Date (); String year = String. format ("% tY", date); String month = String. format ("% tB", date); String day = String. format ("% td", date); System. out. println ("this year is:" + year + "year"); System. out. println ("now:" + month); System. out. println ("today is:" + day + ");}}


This article is from the "ToBeContinued" blog, please be sure to keep this source http://leexy.blog.51cto.com/4136883/1305474

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.