Java numeric and currency formatting

Source: Internet
Author: User

 

 Package com. tq365.util;

Import java. Text. numberformat;
Import java. util. locale;

Public class formattest {

Public static void main (string [] ARGs ){
// No formatted output
Double D = 10000.0/3.0;
System. Out. println ("No formatted output:" + D );

Numberformat = numberformat. getnumberinstance ();
// Retain four decimal places
// Numberformat. setmaximumfractiondigits (4 );
// The first six digits of the decimal point
// Numberformat. setminimumintegerdigits (6 );
// System. Out. println (numberformat. Format (d ));
// The local default format is 3,333.333.
System. Out. println (numberformat. Format (d ));

// Use the local default format to output the currency value
Numberformat currentformat = numberformat. getcurrencyinstance ();
// ¥3,333.33
System. Out. println ("Local default format output currency value:" + currentformat. Format (d ));

// Use the local default format to output the percentage
Numberformat percentformat = numberformat. getpercentinstance ();
System. Out. println ("Local default format output percentage:" + percentformat. Format (d ));

// Use the U.S. currency output form
Numberformat currentformatus = numberformat. getcurrencyinstance (locale. US );
// $3,333.33
System. Out. println ("U.S. currency output form:" + currentformatus. Format (d ));

}
}

 

Decimalformat

  1. Import java. Text. decimalformat;
  2. Public class testnumberformat {
  3. Public static void main (string [] ARGs ){
  4. Double Pi = 3.1415927; // circumference Rate
  5. // Returns an integer.
  6. System. Out. println (New decimalformat ("0"). Format (PI); // 3
  7. // Take one integer and two decimal places
  8. System. Out. println (New decimalformat ("0.00"). Format (PI); // 3.14
  9. // Take two integers and three decimal places. The less integer is filled with 0.
  10. System. Out. println (New decimalformat ("00.000"). Format (PI); // 03.142
  11. // Obtain all integer parts
  12. System. Out. println (New decimalformat ("#"). Format (PI); // 3
  13. // Count by percentage and take two decimal places
  14. System. Out. println (New decimalformat ("#. ### %"). Format (PI); // 314.16%
  15. Long c = 299792458; // light speed
  16. // Display as a scientific notation and take five decimal places
  17. System. Out. println (New decimalformat ("#. ##### E0"). Format (c); // 2.99792e8
  18. // Display the scientific notation of two integers with four decimal places
  19. System. Out. println (New decimalformat ("00. #### E0"). Format (c); // 29.9792e7
  20. // Each three digits are separated by commas.
  21. System. Out. println (New decimalformat (", ###"). Format (c); // 299,792,458
  22. // Embed the format into the text
  23. System. Out. println (New decimalformat ("the speed of light is every second, ### meters. "). Format (c ));
  24. }
  25. }

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.