3

我有下一个孤立的测试用例场景

@SMDMethod
public BigInteger getSomeReallyBigInteger() {
    return new BigInteger("154456875042019001");
}

这是 struts.xml 中的操作

    <action name="DataSourceRpc" class="isoblock.struts2.action.DataSourceAction" method="smd">
        <interceptor-ref name="json">
            <param name="enableSMD">true</param> 
        </interceptor-ref>
        <result type="json">
            <param name="enableSMD">true</param>
        </result>
    </action>

我使用 JSON-RPC 实现(使用 dojo-rpc)调用 SMD 函数,这是失败的,

当我调用最后一个函数时,结果回调它:

  • 154456875042019000

代替

  • 154456875042019001

这只发生在大数字(全部有 17 个或更多 dijit),我使用struts2-json-plugin-2.3.8.jar(最新)

所以,这是一个struts2错误??

问候,

4

1 回答 1

2

问题是 Javascript 中的数字是双精度浮点数,不能准确表示 154456875042019001。双精度浮点数的精度为 15-17 位,而您有 18 位。当转换为浮点数并再次返回时,会丢失一些精度。

例如,在 Perl 中:

$a=154456875042019001.0;
printf "%20d",$a;

输出

154456875042019008

更多细节:

15445687504201900110的十六进制表示形式是0x0224bdb5a1ff16b9,它包含 58 个有效位(在二进制中它以 开头0000 0010 0010 0100 ...,因此 64 减去 6 个前导零位)。双精度浮点数具有 52 位精度,因此在将 64 位转换longdouble.

于 2013-05-31T23:27:15.617 回答