在Ajax中处理非常大的长数的最佳方法是什么? [英] Best way to deal with very large Long numbers in Ajax?

查看:90
本文介绍了在Ajax中处理非常大的长数的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Javascript将所有数字表示为双精度浮点数。这意味着它在处理64位Java Long数据类型的最高端的数字时会丢失精度 - 17位数后的任何数据。例如,数字:

Javascript represents all numbers as double-precision floating-point. This means it loses precision when dealing with numbers at the very highest end of the 64 bit Java Long datatype -- anything after 17 digits. For example, the number:

714341252076979033

...变为:

714341252076979100

我的数据库使用长ID,有些恰好位于危险区域。我可以更改数据库中的违规值,但在我的应用程序中这很难。相反,现在我非常费力地确保服务器在所有ajax响应中将Long ID编码为字符串。

My database uses long IDs and some happen to be in the danger zone. I could change the offending values in the database, but that'd be difficult in my application. Instead, right now I rather laboriously ensure the server encodes Long IDs as Strings in all ajax responses.

但是,我更愿意在Javascript中处理这个问题。我的问题:是否有强制JSON解析将数字视为字符串的最佳做法?

However, I'd prefer to deal with this in the Javascript. My question: is there a best practice for coercing JSON parsing to treat a number as a string?

推荐答案

你必须发送你的值作为字符串(即用引号括起来),以确保Javascript将它们视为字符串而不是数字。

You do have to send your values as strings (i.e. enclosed in quotes) to ensure that Javascript will treat them as strings instead of numbers.

我无法解决这个问题。

这篇关于在Ajax中处理非常大的长数的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆