用javaScript进行货币操作?
我正在使用 JavaScript 创建一个预算应用程序。我必须让 javascript 完成大部分操作。因此,我有一些控件,用户可以在其中更改类别所需的金额,并且我的应用程序会显示子类别的新金额。 所以如果我有 + 汽车 50.23 美元 - 汽油 30.25 美元 - 维护 6.27 美元 - 保险 10.02 美元 - ...
因此,如果用户将汽车 50.23 更改为 90.00,汽油、维护、保险等金额将反映其父类别的增加百分比。
问题是,经过几次操作后,我失去了精度,并产生了一些令人讨厌的错误。大多数时候,这些数字不会相加(正如它们应该的那样)。多次更改 auto 的金额然后将其设置为 0 后,其他金额并不总是变为 0,有时它们会低于 0,给我一个 NaN,所以如果我后来将 Auto 更改为大于 0 的值,我的 NaN 将保持 NaN。
我刚刚听到有人说 javascript 中的分数是近似值,这是另一个问题。我试图用谷歌来看看这是否是事实,但找不到任何东西(也许我没有寻找正确的术语)
我确信你们中的一些人遇到了类似的问题,你会建议什么? 有人建议将我的原始数字乘以 100,然后进行所有操作,但我担心这不会帮助我摆脱精度损失的问题。
有什么想法吗?
我将感谢您的帮助!
I'm creating a budget application with javascript. I have to let javascript do most of the operations. So I have some controls where the user changes the desired amount for a category and my application shows the new amounts for the subcategories.
So If I have
+ Auto $50.23
- Gas $30.25
- Maintenance $6.27
- Insurance $10.02
- ...
So if the user changes Auto 50.23 to 90.00, the amounts for Gas, Maintenance, Insurance, etc would reflect the % increase in their parent category.
The problem is that after a few operations I loose precision and it creates some nasty bugs. Most of the times the numbers don't add up (as they should). After changing the amount for auto a few times and then set it to 0, the other amounts don't always go to 0, and sometimes they go below 0 giving me a NaN, so if I later change Auto for something greater than 0, my NaN will stay NaN.
I just heard somebody say that fractions in javascript are approximations which is another problem. I tried to google to see if this is a fact, but couldn't find anything (maybe I didn't look for the right terms)
I'm sure some of you have ran into similar problems, what would you suggest?
Somebody suggested to multiply my original number by 100 and then do all operations, but I'm afraid that this won't help me get away with the problem of loosing precision.
Any ideas?
I will appreciate your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
二进制浮点数无法处理小数,因此 0.1 + 0.2 不等于 0.3。这是
JavaScript 中最常报告的错误,这是采用
IEEE 二进制浮点运算标准 (IEEE 754)。该标准非常适合许多人
应用程序,但它违反了你在中学学到的关于数字的大部分知识。幸运的是,
浮点中的整数运算是精确的,因此可以通过缩放来避免小数表示错误。
例如,美元价值可以通过乘以 100 转换为整数美分价值。然后
可以准确添加。总和可以除以 100 以换算回美元。人们有一个
当他们数钱时,他们会合理地期望结果是准确的。
“JavaScript:Douglas Crockford 的优秀部分。版权所有 2008 Yahoo! Inc.,
978-0-596-51774-8。”
Binary floating-point numbers are inept at handling decimal fractions, so 0.1 + 0.2 is not equal to 0.3. This is
the most frequently reported bug in JavaScript, and it is an intentional consequence of having adopted the
IEEE Standard for Binary Floating-Point Arithmetic (IEEE 754). This standard is well-suited for many
applications, but it violates most of the things you learned about numbers in middle school. Fortunately,
integer arithmetic in floating point is exact, so decimal representation errors can be avoided by scaling.
For example, dollar values can be converted to whole cents values by multiplying them by 100. The cents then
can be accurately added. The sum can be divided by 100 to convert back into dollars. People have a
reasonable expectation when they count money that the results will be exact.
"JavaScript: The Good Parts by Douglas Crockford. Copyright 2008 Yahoo! Inc.,
978-0-596-51774-8."
始终使用货币在内部将其作为整数进行跟踪。所有运算都应该对整数进行。仅显示小数点。
如果您正在考虑使用小数点来记录金钱,看看这个小提琴。
Always with currency keep track of it as integers internally. All operations should be done on integers. Only have the decimal for display.
check out this fiddle if you are thinking about using decimal points to keep track of money.