使用javaScript进行货币操作? [英] Currency operations with javaScript?

查看:175
本文介绍了使用javaScript进行货币操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用javascript创建预算应用程序。我必须让javascript完成大部分操作。所以我有一些控件,用户可以更改类别的所需金额,我的应用程序显示子类别的新金额。
所以如果我有
+自动$ 50.23
- 气体$ 30.25
- 维护$ 6.27
- 保险$ 10.02
- ...

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 - ...

因此,如果用户将Auto 50.23更改为90.00,则Gas,Maintenance,Insurance等的金额将反映其父类别的增加百分比。

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.

问题是经过一些操作后,我失去了精度,并产生了一些令人讨厌的错误。大多数情况下,数字不会加起来(正如他们应该的那样)。在将auto的数量更改为几次然后将其设置为0之后,其他数量并不总是变为0,有时它们会低于0给我一个NaN,所以如果我稍后将大于0的内容更改为Auto,我的NaN将保留NaN。

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.

我刚刚听到有人说javascript中的分数是近似值,这是另一个问题。我试图谷歌看看这是不是事实,但找不到任何东西(也许我没有找到合适的条款)

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)

我肯定有些你遇到过类似的问题,你会建议什么?
有人建议将我的原始数字乘以100,然后进行所有操作,但我担心这不会帮助我摆脱失去精确度的问题。

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.

任何想法?

我将非常感谢您的帮助!

I will appreciate your help!

推荐答案

二进制浮点数在处理小数分数时无效,因此0.1 + 0.2不等于0.3。这是
是JavaScript中最常报告的错误,它是采用
IEEE二进制浮点运算标准(IEEE 754)的故意结果。这个标准非常适合许多
申请,但它违反了你在中学学到的大部分数据。幸运的是,浮点数
整数运算是精确的,因此通过缩放可以避免十进制表示错误。
例如,美元价值可以通过将它们乘以100来转换为整美分。然后可以准确地添加美分,即
。总和可以除以100以转换回美元。当人们计算结果是准确的时候,人们有一个
的合理期望。

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:Douglas Crockford的好部分。版权所有2008 Yahoo! Inc.,
978-0-596-51774-8。

这篇关于使用javaScript进行货币操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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