forked from leavesCZY/AndroidGuide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecimalFormatUtil.java
35 lines (31 loc) · 1.21 KB
/
DecimalFormatUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.company.utils;
import java.text.DecimalFormat;
public class DecimalFormatUtil {
public static void main(String[] args) {
double pi = 3.1415;
//3
System.out.println(new DecimalFormat("0").format(pi));
//3.14
System.out.println(new DecimalFormat("0.00").format(pi));
//3.14150
System.out.println(new DecimalFormat("0.00000").format(pi));
//03.142
System.out.println(new DecimalFormat("00.000").format(pi));
//3
System.out.println(new DecimalFormat("#").format(pi));
//3
System.out.println(new DecimalFormat("##").format(pi));
//3.1
System.out.println(new DecimalFormat("##.#").format(pi));
//314.16%
System.out.println(new DecimalFormat("#.##%").format(pi));
//圆周率为 3.14 米
System.out.println(new DecimalFormat("圆周率为 ###.00 米").format(pi));
//113.1
System.out.println(new DecimalFormat("##.#").format(113.1415));
//113.2
System.out.println(new DecimalFormat("00.#").format(113.1615));
//11211.210012
System.out.println(new BigDecimal("0011211.210012000").stripTrailingZeros().toPlainString());
}
}