json-lib对于浮点数出现精度问题

使用json-lib-2.4-jdk15.jar在json与pojo转换时遇到浮点数精度丢失问题,将版本回退到2.3后恢复正常。问题源于2.4版中使用了createFloat(),而2.3版使用new Double()。测试显示,2.3版本能保持浮点数的正确精度。目前有两种解决办法:降级json-lib版本或全部使用字符串类型存储浮点数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目使用json-lib-2.4-jdk15.jar进行json<->pojo之间转换,今天发现一个悲剧问题:

String jsonString = "[{\"amount\":670526.32},{\"amount\":29473.68}]";
JSONObject jsonObject = JSONObject.fromObject(jsonString);

结果发现第一个数值转换后为670526.3,精度出现问题,网上搜索一番,没找到原因,蛮试试降低版本json-lib-2.3-jdk15.jar,竟然正确,看来这小版本变动大有文章,反编译看源码,先看看2.4的
JSONObject.java

private static JSONObject _fromJSONTokener(JSONTokener tokener, JsonConfig jsonConfig)
{
......
v = tokener.nextValue(jsonConfig);
......
}

JSONTokener.java

public Object nextValue(JsonConfig jsonConfig)
{
......
if (((b >= '0') && (b <= '9')) || ((((b == '.') || (b == '-') || (b == '+'))) && ((
(b != '0') || (
(s.length() > 2) && (((s.charAt(1) == 'x') || (s.charAt(1) == 'X'))))))));
try
{
return new Integer(Integer.parseInt(s.substring(2), 16));
}
catch (Exception e)
{
try
{
return new Integer(Integer.parseInt(s, 8));
}
catch (Exception e)
{
try
{
return NumberUtils.createNumber(s);
} catch (Exception e) {
return s;
}

if ((JSONUtils.isFunctionHeader(s)) || (JSONUtils.isFunction(s)))
return s;
}
......
}

NumberUtils.java

public static Number createNumber(String str)
throws NumberFormatException
{
......
try
{
return createInteger(str);
}
catch (NumberFormatException allZeros)
{
try {
return createLong(str);
}
catch (NumberFormatException nfe)
{
return createBigInteger(str);
}

boolean allZeros = (isAllZeros(mant)) && (isAllZeros(exp));
try {
Float f = createFloat(str);
if ((!(f.isInfinite())) && (((f.floatValue() != 0.0F) || (allZeros))))
return f;
}
catch (NumberFormatException nfe)
{
}
try {
Double d = createDouble(str);
if ((!(d.isInfinite())) && (((d.doubleValue() != 0.0D) || (allZeros))))
return d;
}
catch (NumberFormatException d)
{
}
}
return createBigDecimal(str);
}

以上看出,对于浮点数字,会先使用createFloat()进行转换,此处也是导致精度出现问题的关键
2.3代码:
JSONTokener.java

public Object nextValue(JsonConfig jsonConfig)
{
......
if (((b >= '0') && (b <= '9')) || ((((b == '.') || (b == '-') || (b == '+'))) && ((
(b != '0') || (
(s.length() > 2) && (((s.charAt(1) == 'x') || (s.charAt(1) == 'X'))))))));
try
{
return new Integer(Integer.parseInt(s.substring(2), 16));
}
catch (Exception e)
{
try
{
return new Integer(Integer.parseInt(s, 8));
}
catch (Exception e)
{
try
{
return new Integer(s);
} catch (Exception e) {
try {
return new Long(s);
} catch (Exception f) {
try {
return new Double(s);
} catch (Exception g) {
return s;
}

if ((JSONUtils.isFunctionHeader(s)) || (JSONUtils.isFunction(s)))
return s;
}
}

以上看出,对于浮点数字,会先使用new Double(s)

最后写个main函数测试:

String a = "670526.01";
System.out.println("aa:"+Float.valueOf(a));
System.out.println("aa:"+new Double(a));

打印结果:
aa:670526.3
aa:670526.32

小版本的差异,出现了如此差异,实在不知说什么好。
目前只能降低版本,不过要多做测试;还有一种方法,全部使用字符串,如String jsonString = "[{\"amount\":\"670526.32\"},{\"amount\":\"29473.68\"}]";
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值