This is the 24th day of my participation in the August Text Challenge.More challenges in August

A practical tip every day, number formatting

String.format is usually used for formatting numbers, but note that there is a DecimalFormat for formatting numbers

Today we’re going to do DecimalFormat

1. DecimalFormat Instruction

DecimalFormat is easy to use, mainly with the help of two placeholders, 0 and #. The difference is that when the number of placeholders is more than the actual number, the prefix 0 is used. # doesn’t need to be completed

This might be a little confusing, but here’s an example

double num = 3.1415926;
System.out.println(new DecimalFormat("000", num));
System.out.println(new DecimalFormat("# # #", num));
Copy the code

The above two outputs are integers only, but the output is different, as follows

003
3
Copy the code

In simple terms, it is 0, mainly used for fixed-length output, for insufficient, prefix complement 0

Integer # decimal

In addition to the basic posture above, it is more common to set the number of digits of integers and decimals

System.out.println(new DecimalFormat("000.00", num));
System.out.println(new DecimalFormat("# # # # #", num));
Copy the code

The output is as follows

003.14
3.14
Copy the code

The percentage

Percentage output is also a common case, and DecimalFormat is easy

System.out.println(new DecimalFormat("000.00", num));
System.out.println(new DecimalFormat("# # # # #", num));
Copy the code

The output is as follows

314.16%
314.16%
Copy the code

Scientific count

In non-professional scenarios, science and technology are less likely

System.out.println(new DecimalFormat(000.00 "e0", num));
System.out.println(new DecimalFormat("###.##E0", num));
Copy the code

The output is as follows

314.16 e-2 3.1416 e0Copy the code

Money style output

The interesting thing about finance-related money output is that it can be easily separated by a comma

double num = 31415926
System.out.println(new DecimalFormat(", # # #", num));
Copy the code

The output is as follows

31415926Copy the code

Embedded template output

Format templates, in addition to the basic 000, ###, can be placed directly in a String, similar to the effect of string.format

Like showing the balance

double num = 31415926
System.out.println(new DecimalFormat("Your balance,###¥", num));
Copy the code

The output is as follows

Your balance is ¥31,415,926Copy the code

Series of blog posts:

  • Practical tip 1: String placeholder replacement -JDK version
  • Practice Tip 2: Array and list are rotated
  • Practice Tip 3: String and container transfer
  • Practical tip 4: Elegant string concatenation implementation
  • Practice tip 5: Hump and underline turn each other
  • Practice Tip 6: Special use of enumeration
  • Practice Tip 7: Sort carefully
  • Practice tip 8: Specify the initial size of the container
  • List. SubList is used incorrectly StackOverflowError
  • Practice Tip # 10: Immutable containers

II. The other

1. A gray Blog:liuyueyi.github.io/hexblog

A gray personal blog, recording all the study and work in the blog, welcome everyone to go to stroll

2. Statement

As far as the letter is not as good, the above content is purely one’s opinion, due to the limited personal ability, it is inevitable that there are omissions and mistakes, if you find bugs or have better suggestions, welcome criticism and correction, don’t hesitate to appreciate

  • Micro Blog address: Small Gray Blog
  • QQ: a gray /3302797840
  • Wechat official account: One Grey Blog