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\"}]";