Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

javascript - 如何处理JavaScript中的浮点数精度?(How to deal with floating point number precision in JavaScript?)

I have the following dummy test script:

(我有以下虚拟测试脚本:)

 function test() { var x = 0.1 * 0.2; document.write(x); } test(); 

This will print the result 0.020000000000000004 while it should just print 0.02 (if you use your calculator).

(这将打印结果0.020000000000000004而应该只打印0.02 (如果使用计算器)。)

As far as I understood this is due to errors in the floating point multiplication precision.

(据我了解,这是由于浮点乘法精度的错误。)

Does anyone have a good solution so that in such case I get the correct result 0.02 ?

(有没有人有一个好的解决方案,这样在这种情况下我可以得到0.02的正确结果?)

I know there are functions like toFixed or rounding would be another possibility, but I'd like to really have the whole number printed without any cutting and rounding.

(我知道还有诸如toFixed或四舍五入之类的函数是另一种可能性,但是我真的想在不进行任何舍入和四舍五入的情况下真正打印出整个数字。)

Just wanted to know if one of you has some nice, elegant solution.

(只是想知道你们中的一个人是否有一些不错的,优雅的解决方案。)

Of course, otherwise I'll round to some 10 digits or so.

(当然,否则我将四舍五入到十位数左右。)

  ask by Juri translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From the Floating-Point Guide :

(从浮点指南 :)

What can I do to avoid this problem?

(我应该怎么做才能避免这个问题?)

That depends on what kind of calculations you're doing.

(这取决于您正在执行哪种计算。)

  • If you really need your results to add up exactly, especially when you work with money: use a special decimal datatype.

    (如果您确实需要精确地将结果相加,尤其是在使用金钱时,请使用特殊的十进制数据类型。)

  • If you just don't want to see all those extra decimal places: simply format your result rounded to a fixed number of decimal places when displaying it.

    (如果您只是不想看到所有这些多余的小数位:只需在显示结果时将结果的格式四舍五入为固定的小数位数即可。)

  • If you have no decimal datatype available, an alternative is to work with integers, eg do money calculations entirely in cents.

    (如果没有可用的十进制数据类型,则替代方法是使用整数,例如,完全用美分进行货币计算。)

    But this is more work and has some drawbacks.

    (但这是更多的工作,并且有一些缺点。)

Note that the first point only applies if you really need specific precise decimal behaviour.

(请注意,只有在您确实需要特定的精确十进制行为时,第一点才适用。)

Most people don't need that, they're just irritated that their programs don't work correctly with numbers like 1/10 without realizing that they wouldn't even blink at the same error if it occurred with 1/3.

(大多数人并不需要它们,只是因为他们的程序无法正确处理1/10之类的数字而感到恼火,却没有意识到如果发生1/3,即使出现相同的错误也不会眨眼。)

If the first point really applies to you, use BigDecimal for JavaScript , which is not elegant at all, but actually solves the problem rather than providing an imperfect workaround.

(如果第一点确实适用于您,请对JavaScript使用BigDecimal ,这一点都不优雅,但实际上可以解决问题,而不是提供不完善的解决方法。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...