hutool 实现 两个字符串 金额相加
Hutool是一个Java工具库,提供了丰富的工具类,包括金额计算的工具类。以下是使用Hutool实现两个字符串金额相加的示例代码:
import cn.hutool.core.util.NumberUtil;
public class Demo {
public static void main(String[] args) {
String str1 = "123.45";
String str2 = "678.90";
double result = NumberUtil.add(Double.parseDouble(str1), Double.parseDouble(str2));
System.out.println(result);
}
}
在上述代码中,我们首先导入了Hutool的NumberUtil工具类,然后定义了两个字符串类型的金额,使用Double.parseDouble方法将字符串转换为double类型,最后使用NumberUtil.add方法将两个金额相加并输出结果。
原文地址: https://www.cveoy.top/t/topic/m6w 著作权归作者所有。请勿转载和采集!