浮点数类型导致金额计算错误复盘总结

📌 业务场景 公司通过爬虫获取界面上的商品价格(例如 2.01 元),需要将其转换为以 分为单位的整数 (即 201)存储到数据库中。 后端接收价格时为 String 类型,最终需转换为 Integer 类型存入数据库。

⚠️ 问题再现 直接使用 Double 转换会因浮点数精度问题导致计算错误:

ini 复制代码
String money = "2.01";
// ❌ 错误示范:使用Double处理金额
Double esMoney = Double.valueOf(money);
System.out.println(esMoney);  // 打印:2.01(表面显示正常)
Double v = esMoney * 100;
System.out.println(v); // 打印:200.99999999999997(误差被放大)
int moneyInt = new Double(esMoney * 100).intValue();
System.out.println(moneyInt); // 最终错误结果:200(正确应为201)

🔍 问题原因 Double 采用二进制存储数据,部分十进制小数(如 2.01)无法被精确表示,只能存储近似值。 当进行乘法运算(如 ×100)时,微小的误差会被放大,导致最终结果出错。

正确解决方案 使用 BigDecimal 处理金额计算(底层基于十进制运算,无精度损失):

ini 复制代码
String money = "2.01";
// ✅ 正确示范:使用BigDecimal处理金额
BigDecimal bigDecimal = new BigDecimal(money); // 注意:必须用String构造,而非double
int moneyInt2 = bigDecimal.multiply(new BigDecimal(100)).intValue();
System.out.println(moneyInt2); // 正确结果:201

📝 核心总结

  1. 处理 金额、高精度浮点数 时,务必使用 BigDecimal 而非 Double/Float
  2. 初始化 BigDecimal 时,必须传入 String 类型参数(避免因 double 本身的精度问题引入误差)。

💻 完整测试代码

ini 复制代码
public static void main(String[] args) {
    String money = "2.01";
    
    // ❌ 错误示范
    Double esMoney = Double.valueOf(money);
    System.out.println(esMoney);  // 2.01
    Double v = esMoney * 100;
    System.out.println(v); // 200.99999999999997
    int moneyInt = new Double(esMoney * 100).intValue();
    System.out.println(moneyInt); // 200
    
    // ✅ 正确示范
    BigDecimal bigDecimal = new BigDecimal(money);
    int moneyInt2 = bigDecimal.multiply(new BigDecimal(100)).intValue();
    System.out.println(moneyInt2); // 201
}
相关推荐
SelectDB几秒前
Apache Doris 中的 Data Trait:性能提速 2 倍的秘密武器
数据库·后端·apache
zhengzizhe9 分钟前
LangGraph4j LangChain4j JAVA 多Agent编排详解
java·后端
程序员鱼皮14 分钟前
又被 Cursor 烧了 1 万块,我麻了。。。
前端·后端·ai·程序员·大模型·编程
福大大架构师每日一题23 分钟前
2025-11-27:为视频标题生成标签。用go语言,给定一个字符串 caption(视频标题),按下面顺序处理并输出一个标签: 1. 将标题中的各个词合并成一
后端
程序员爱钓鱼24 分钟前
Go语言 OCR 常用识别库与实战指南
后端·go·trae
tonydf30 分钟前
动态表单之后:如何构建一个PDF 打印引擎?
后端
allbs32 分钟前
spring boot项目excel导出功能封装——4.导入
spring boot·后端·excel
用户693717500138440 分钟前
11.Kotlin 类:继承控制的关键 ——final 与 open 修饰符
android·后端·kotlin
用户693717500138443 分钟前
10.Kotlin 类:延迟初始化:lateinit 与 by lazy 的对决
android·后端·kotlin
稚辉君.MCA_P8_Java1 小时前
通义 Go 语言实现的插入排序(Insertion Sort)
数据结构·后端·算法·架构·golang