Talk about the usage of DecimalFormat and the meaning of each symbol
- 2021-11-24 01:33:49
- OfStack
The Usage of DecimalFormat and the Significance of Each Symbol
符号 | 位置 | 本地化? | 含义 |
0 | 数字 | 是 | 阿拉伯数字 |
# | 数字字 | 是 | 阿拉伯数字,如果不存在则显示为空 |
. | 数字 | 是 | 小数分隔符或货币小数分隔符 |
- | 数字 | 是 | 减号 |
, | 数字 | 是 | 分组分隔符 |
E | 数字 | 是 | 分隔科学计数法中的尾数和指数。在前缀或后缀中无需加引号。 |
; | 子模式边界 | 是 | 分隔正数和负数子模式 |
% | 前缀或后缀 | 是 | 乘以 100 并显示为百分数 |
/u2030 | 前缀或后缀 | 是 | 乘以 1000 并显示为千分数 |
¤(/u00A4) | 前缀或后缀 | 否 | 货币记号,由货币符号替换。如果两个同时出现,则用国际货币符号替换。如果出现在某个模式中,则使用货币小数分隔符,而不使用小数分隔符。 |
' | 前缀或后缀 | 否 | 用于在前缀或或后缀中为特殊字符加引号,例如 "'#'#" 将 123 格式化为 "#123"。要创建单引号本身,请连续使用两个单引号:"# o''clock"。 |
DecimalFormat format = new DecimalFormat("###,####.000");
System.out.println(format.format(111111123456.1227222)); // 1111,1112,3456.123
Locale.setDefault(Locale.US);
DecimalFormat usFormat = new DecimalFormat("###,###.000");
System.out.println(usFormat.format(111111123456.1227222)); // 111,111,123,456.123
DecimalFormat addPattenFormat = new DecimalFormat();
addPattenFormat.applyPattern("##,###.000");
System.out.println(addPattenFormat.format(111111123456.1227)); // 111,111,123,456.123
DecimalFormat zhiFormat = new DecimalFormat();
zhiFormat.applyPattern("0.000E0000");
System.out.println(zhiFormat.format(10000)); // 1.000E0004
System.out.println(zhiFormat.format(12345678.345)); // 1.235E0007
DecimalFormat percentFormat = new DecimalFormat();
percentFormat.applyPattern("#0.000%");
System.out.println(percentFormat.format(0.3052222)); // 30.522%
Considerations when using DecimalFormat
import java.text.DecimalFormat;
public class Format {
public static void main(String[] args) {
double ss=12345.97468;
double ss2=0.23;
DecimalFormat ff=new DecimalFormat("#,###,###.######"); The green area is the reserved decimal place ( 4 Shed 5 In) ---- But it won't be guaranteed Leaving the end 0 ) -------------------- There is no decimal place if it is an integer
DecimalFormat ff2=new DecimalFormat("#,###,###.0000"); // Retain after decimal point 4 Decimal places (leaving the last 0 ) // Similar to round() Function ---4 Shed 5 Into
DecimalFormat ff3=new DecimalFormat("#,###,###.00000"); // Retain after decimal point 5 Decimal places (leaving the last 0 )
DecimalFormat ff4=new DecimalFormat("#,###,###0.0000"); // Retain after decimal point 4 Decimal places (leaving the last 0 )
DecimalFormat ff5=new DecimalFormat("#,###,###0.00000"); // Retain after decimal point 5 Decimal places (leaving the last 0 )
System.out.println("#,###,###.######: "+ff.format(ss));
System.out.println("#,###,###.0000 : "+ff2.format(ss)); // Similar to round() Function ---4 Shed 5 Into
System.out.println("#,###,###.00000 : "+ff3.format(ss));
System.out.println("---------------------------------------------------");
System.out.println("f111#,###,###.######: "+ff.format(ss2));
System.out.println("f222#,###,###.0000 : "+ff2.format(ss2));
System.out.println("f333#,###,###.00000 : "+ff3.format(ss2));
System.out.println("---------------------------------------------------");
System.out.println("f111#,###,###.######: "+ff.format(ss2));
System.out.println("f444#,###,###.0000 : "+ff4.format(ss2));
System.out.println("f555#,###,###.00000 : "+ff5.format(ss2));
System.out.println("---------------------------------------------------");
System.out.println("sss#,###,###.0000 : "+ff4.format(ss));
System.out.println("sss#,###,###.00000 : "+ff5.format(ss));
}
}
Results:
#,###,###.######: 12,345.97468
#,###,###.0000 : 12,345.9747
#,###,###.00000 : 12,345.97468
---------------------------------------------------
f111#,###,###.######: 0.23
f222 #, # # #, # # #. 0000:. 2300 (Problem)
f333 #, # # #, # # #. 00000:. 23000 (Problem)
---------------------------------------------------
f111#,###,###.######: 0.23
f444#,###,###.0000 : 0.2300
f555#,###,###.00000 : 0.23000---------------------------------------------------
sss #, # # #, # # #. 0000: 1,234 5.9747 (Problem)--Problem with kilobit position
sss #, # # #, # # #. 00000: 1,234 5.97468 (Problem)--Problem with kilobit position
Let's sum up 1
(1) double ss2=0. 23; Use when:
DecimalFormat ff4=new DecimalFormat("#,###,###0.0000"); // Retain after decimal point 4 Decimal places (leaving the last 0 )
(2) double ss=12345.97468; Use when:
DecimalFormat ff4=new DecimalFormat("#,###,###.0000"); // Retain after decimal point 4 Decimal places (leaving the last 0 )