You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.3 KiB
48 lines
1.3 KiB
package com.spring.modules.part.util;
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
public class UtilsClass {
|
|
|
|
/**
|
|
* 字符串转BigDecimal
|
|
* @param str
|
|
* @return
|
|
*/
|
|
public static BigDecimal stringToBigDecimal(String str) {
|
|
if (str == null || str.trim().isEmpty()) {
|
|
return BigDecimal.ZERO; // 或者返回 BigDecimal.ZERO,视业务需求而定
|
|
}
|
|
try {
|
|
return new BigDecimal(str.trim());
|
|
} catch (Exception ex) {
|
|
System.err.println("无法解析的数值: " + str);
|
|
return BigDecimal.ZERO; // 或抛异常,或返回 null
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 字符串转Integer
|
|
* @param str
|
|
* @return
|
|
*/
|
|
public static int stringToInteger(String str) {
|
|
// 初始化一个默认的整数值
|
|
int number = 0;
|
|
// 检查传入的字符串是否为 null
|
|
if (str == null) {
|
|
return number; // 如果是 null,则返回默认值
|
|
}
|
|
try {
|
|
// 使用字符串创建 Integer 对象
|
|
number = Integer.parseInt(str);
|
|
} catch (NumberFormatException e) {
|
|
// 处理非法格式的情况
|
|
System.err.println("无效的整数值: " + str);
|
|
// 返回默认值
|
|
number = 0;
|
|
}
|
|
return number;
|
|
}
|
|
|
|
}
|