Fastjson: FastJson BigDecimal 序列化问题

Created on 14 Feb 2019  ·  3Comments  ·  Source: alibaba/fastjson

使用版本 fastjson 1.2.56
测试代码:
BigDecimal money = new BigDecimal(200.152);
String json = JSON.toJSONString(money);
System.out.println(json);

实际输出:
200.15199999999998681232682429254055023193359375

image
请问下有什么好的解决方案么?

question

Most helpful comment

源头在与数本身就有精度问题

构造里传 double 有鬼的
换字符串

All 3 comments

源头在与数本身就有精度问题

构造里传 double 有鬼的
换字符串

@lemon-machine 说的对,这个不是fastjson的问题,是你初始化BigDecimal的问题。
new BigDecimal("200.152") 这样写试试看

@wenshao 好的,谢谢

Was this page helpful?
0 / 5 - 0 ratings