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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package cn.wisenergy.chnmuseum.party.common.util;
import java.math.BigDecimal;
import java.text.DecimalFormat;
public class CurrencyMathUtil {
private static int SCALE = 18;
public static int compare(String str1, String str2) {
BigDecimal a = new BigDecimal(str1).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
BigDecimal b = new BigDecimal(str2).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
return a.compareTo(b);
}
/**
* 乘法
* @param str1
* @param str2
* @return
*/
public static String multiply(String str1, String str2) {
BigDecimal a = new BigDecimal(str1).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
BigDecimal b = new BigDecimal(str2).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
BigDecimal c = a.multiply(b).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
return c.stripTrailingZeros().toPlainString();
}
/**
* 除法
* @param str1
* @param str2
* @return
*/
public static String divide(String str1, String str2) {
BigDecimal a = new BigDecimal(str1).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
BigDecimal b = new BigDecimal(str2).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
BigDecimal c = a.divide(b, SCALE, BigDecimal.ROUND_HALF_DOWN);
return c.stripTrailingZeros().toPlainString();
}
/**
* 加法
* @param str1
* @param str2
* @return
*/
public static String add(String str1, String str2) {
BigDecimal a = new BigDecimal(str1).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
BigDecimal b = new BigDecimal(str2).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
BigDecimal c = a.add(b).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
return c.stripTrailingZeros().toPlainString();
}
public static String add(Integer str1, Integer str2) {
BigDecimal a = new BigDecimal(str1.toString()).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
BigDecimal b = new BigDecimal(str2.toString()).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
BigDecimal c = a.add(b).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
return c.stripTrailingZeros().toPlainString();
}
public static Double add(Double d1, Double d2) {
BigDecimal a = new BigDecimal(d1.toString()).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
BigDecimal b = new BigDecimal(d2.toString()).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
BigDecimal c = a.add(b).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
return c.stripTrailingZeros().doubleValue();
}
/**
* 减法
* @param str1
* @param str2
* @return
*/
public static String subtract(String str1, String str2) {
BigDecimal a = new BigDecimal(str1).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
BigDecimal b = new BigDecimal(str2).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
BigDecimal c = a.subtract(b).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
return c.stripTrailingZeros().toPlainString();
}
public static Double subtract(Double str1, Double str2) {
BigDecimal a = new BigDecimal(str1.toString()).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
BigDecimal b = new BigDecimal(str2.toString()).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
BigDecimal c = a.subtract(b).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
return c.stripTrailingZeros().doubleValue();
}
public static String gweiToWei(String gwei) {
BigDecimal a = new BigDecimal(gwei).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
BigDecimal b = new BigDecimal("1000000000").setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
BigDecimal c = a.multiply(b).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
return c.stripTrailingZeros().toPlainString();
}
public static String ethToWei(String eth) {
BigDecimal a = new BigDecimal(eth).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
BigDecimal b = new BigDecimal("1000000000000000000").setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
BigDecimal c = a.multiply(b).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
return c.stripTrailingZeros().toPlainString();
}
public static String ethToGwei(String eth) {
BigDecimal a = new BigDecimal(eth).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
BigDecimal b = new BigDecimal("1000000000").setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
BigDecimal c = a.multiply(b).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
return c.stripTrailingZeros().toPlainString();
}
public static String gweiToEth(String gwei) {
BigDecimal a = new BigDecimal(gwei).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
BigDecimal b = new BigDecimal("1000000000").setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
BigDecimal c = a.divide(b).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
return c.stripTrailingZeros().toPlainString();
}
public static String weiToEth(String gwei) {
BigDecimal a = new BigDecimal(gwei).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
BigDecimal b = new BigDecimal("1000000000000000000").setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
BigDecimal c = a.divide(b).setScale(SCALE, BigDecimal.ROUND_HALF_DOWN);
return c.stripTrailingZeros().toPlainString();
}
public static String roundByScale(double v, int scale) {
if (scale < 0) {
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
if (scale == 0) {
return new DecimalFormat("0").format(v);
}
String formatStr = "0.";
for (int i = 0; i < scale; i++) {
formatStr = formatStr + "0";
}
return new DecimalFormat(formatStr).format(v);
}
}