"Java Data formatting" uses DecimalFormat to format float and double

Source: Internet
Author: User

Formatting consists of the following:
    • Basic usage
    • Money format;
    • Scientific counting method;
    • Percent counting method;
    • Embed text;
  
 
  1. package com.sssppp.NumberFormat;
  2. import java.text.DecimalFormat;
  3. public class DecimalFormatExample {
  4. public static void main(String arg[]) {
  5. double piD=3.1415926;
  6. float piF=3.1415926f;
  7. System.out.println("----基本用法----");
  8. DecimalFormat formatter = new DecimalFormat("#.###");
  9. System.out.println("#.###");
  10. System.out.println(formatter.format(piD));
  11. System.out.println(formatter.format(piF));
  12. System.out.println();
  13. formatter.applyPattern("0.00000");
  14. System.out.println("0.00000");
  15. System.out.println(formatter.format(piD));
  16. System.out.println(formatter.format(piF));
  17. System.out.println();
  18. //金钱格式
  19. System.out.println("----金钱格式----");
  20. formatter.applyPattern("#,###,###");
  21. System.out.println("#,###,###");
  22. System.out.println(formatter.format(1300));
  23. System.out.println(formatter.format(123467898));
  24. System.out.println();
  25. formatter.applyPattern("‘$‘#,###,###");
  26. System.out.println("‘$‘#,###,###");
  27. System.out.println(formatter.format(1300));
  28. System.out.println(formatter.format(20000));
  29. System.out.println();
  30. //科学计数法
  31. System.out.println("----科学计数法----");
  32. formatter.applyPattern("#.#####E0");
  33. System.out.println("#.#####E0");
  34. System.out.println(formatter.format(299792458));
  35. formatter.applyPattern("00.####E0");
  36. System.out.println("00.####E0");
  37. System.out.println(formatter.format(299792458));
  38. System.out.println();
  39. System.out.println("----嵌入文本----");
  40. formatter.applyPattern("嵌入文本:#.#####E0");
  41. System.out.println("嵌入文本:#.#####E0");
  42. System.out.println(formatter.format(299792458));
  43. System.out.println();
  44. System.out.println("----以百分比方式计数----");
  45. formatter.applyPattern("#.##%");
  46. System.out.println("#.##%");
  47. System.out.println(formatter.format(0.12));
  48. System.out.println();
  49. }
  50. }
Output Result:
  
 
  1. ----基本用法----
  2. #.###
  3. 3.142
  4. 3.142
  5. 0.00000
  6. 3.14159
  7. 3.14159
  8. ----金钱格式----
  9. #,###,###
  10. 1,300
  11. 123,467,898
  12. ‘$‘#,###,###
  13. $1,300
  14. $20,000
  15. ----科学计数法----
  16. #.#####E0
  17. 2.99792E8
  18. 00.####E0
  19. 29.9792E7
  20. ----嵌入文本----
  21. 嵌入文本:#.#####E0
  22. 嵌入文本:2.99792E8
  23. ----以百分比方式计数----
  24. #.##%
  25. 12%









From for notes (Wiz)

"Java Data formatting" uses DecimalFormat to format float and double

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.