DigitalUtils.java
3.5 KB
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
package com.skua.tool.util;
import org.apache.commons.lang3.StringUtils;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* <pre>
* 数字工具类
* </pre>
*
* @author sonin
* @version 1.0 2022/4/20 17:27
*/
public class DigitalUtils {
/**
* 计算保留n位小数
*
* @return
*/
public static String nPoint(Object src, int n) {
if (src == null) {
return String.format("%." + n + "f", 0D);
} else if (src instanceof Double) {
return String.format("%." + n + "f", src);
} else if (src instanceof String) {
if (isNumeric(src.toString())) {
return String.format("%." + n + "f", Double.parseDouble("" + src));
} else {
return String.format("%." + n + "f", 0D);
}
}
return "" + src;
}
/**
* 加法 保留两位小数
* @return
*/
public static String add(String val1 , String val2){
String resultStr = "0";
if(val1 != null ){
BigDecimal original = new BigDecimal(val1);
BigDecimal num2 = new BigDecimal(val2);
// BigDecimal rounded = original.multiply( num2).setScale(2, BigDecimal.ROUND_HALF_UP);
resultStr = original.add( num2 ).toString();
}
return resultStr;
}
/**
* 乘法 保留两位小数
* @return
*/
public static String multiply(String multiplier , String multiplicand){
String resultStr = "0";
if(multiplier != null ){
BigDecimal original = new BigDecimal(multiplier);
BigDecimal num2 = new BigDecimal(multiplicand);
// BigDecimal rounded = original.multiply( num2).setScale(2, BigDecimal.ROUND_HALF_UP);
resultStr = original.multiply( num2 ).toString();
}
return resultStr;
}
/**
* 除法 保留两位小数
* @param dividend 请输入被除数
* @param divisor 请输入除数
* @return
*/
public static String division(String dividend , String divisor ){
String resultStr = dividend;
if( dividend != null && dividend.length()>0 && divisor != null && divisor.length()>0){
double dividendD = Double.parseDouble(dividend);
double divisorD = Double.parseDouble(divisor);
if(divisorD != 0 ){
double result = dividendD /divisorD;
BigDecimal bd = new BigDecimal(result);
BigDecimal roundedResult = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
resultStr = roundedResult.toString();
}
}
//System.out.print("请输入被除数:");double dividend =scanner.nextDouble();
// System.out.print("请输入除数:");double divisor =scanner.nextDouble();
return resultStr;
}
/**
* 判断字符串是否是数字类型
*
* @param str
* @return
*/
public static boolean isNumeric(String str) {
Pattern pattern = Pattern.compile("-?[0-9]+.?[0-9]*");
// 科学计数法
Pattern pattern2 = Pattern.compile("^[+-]?[\\d]+([.][\\d]*)?([Ee][+-]?[\\d]+)?$");
if (StringUtils.isEmpty(str)) {
return false;
} else {
Matcher isNum = pattern.matcher(str);
Matcher isNum2 = pattern2.matcher(str);
return isNum.matches() || isNum2.matches();
}
}
}