java struts2奇怪的序列化行为(自行舍入大数字)
我有下一个单独的测试用例场景java struts2奇怪的序列化行为(自行舍入大数字),java,ajax,struts2,biginteger,json-rpc,Java,Ajax,Struts2,Biginteger,Json Rpc,我有下一个单独的测试用例场景 @SMDMethod public BigInteger getSomeReallyBigInteger() { return new BigInteger("154456875042019001"); } 这是struts.xml上的操作 <action name="DataSourceRpc" class="isoblock.struts2.action.DataSourceAction" method="smd">
@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
您好,问题在于Javascript中的数字是双精度浮点,无法准确表示154456875042019001。双精度浮点数有15-17位精度,您有18位精度。当转换为浮点数并再次返回时,会丢失一些精度 例如,在Perl中:
$a=154456875042019001.0;
printf "%20d",$a;
输出
154456875042019008
进一步详情:
154456885042019001
10的十六进制表示形式是0x0224bdb5a1ff16b9
,它包含58个有效位(在二进制中它开始0000 0010 0010 0100…
,因此64减去6个前导零位)。双精度浮点具有52位精度,因此在将64位long
转换为Double
hm时,一些位会丢失,因此这意味着我必须返回一个字符串而不是一个数字??谢谢:)那将是一种方法,而且可能是最好的方法。