Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
71 lines (60 loc) · 1.82 KB

File metadata and controls

71 lines (60 loc) · 1.82 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package math;
import java.math.BigDecimal;
/**
* Created by yahier on 12/5/18.
*/
public class TwoDecimalTest {
public static void main(String[] args) {
//get2Decimal(saveTwoDecimal(0));
//get2Decimal(saveTwoDecimal(1.1));
get2Decimal(saveTwoDecimal(1.11));
//get2Decimal(saveTwoDecimal(1.111));
get2Decimal("3");
get2Decimal("3.1");
get2Decimal("3.22");
get2Decimal("3.333");
get2Decimal("3.4444");
}
/**
* 保留两位小数(向上取整)
* 适用于回收端
*/
public static String saveTwoDecimal(double value) {
Double num = 0.0;
BigDecimal bd = null;
try {
bd = BigDecimal.valueOf(value);
bd = bd.setScale(2, BigDecimal.ROUND_UP);
} catch (Exception e) {
e.printStackTrace();
}
if (bd != null) {
num = bd.doubleValue();
}
String newNum = String.valueOf(num);
System.out.println(String.format("old=%s|new=%s", value, newNum));
return newNum;
}
/**
* 转换成 带两位小数
* 没有小数或者位数不够时,进行填充;过长,则截取
*/
private static String get2Decimal(String source) {
int indexPoint = source.indexOf(".");
int length = source.length();
if (indexPoint < 0) {
source = source + ".00";
} else if (indexPoint >= 0) {
int addZeroSize = 3 - (length - indexPoint);
if (addZeroSize >= 0) {
for (int i = 0; i < addZeroSize; i++) {
source = source + "0";
}
} else {
source = source.substring(0, indexPoint + 3);
}
}
System.out.println("result:" + source);
return source;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.