javascript 在 js 中计算百分比
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16489275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculating percentages in js
提问by James
I'm trying to create a simple percentage calculator using javascript.
我正在尝试使用 javascript 创建一个简单的百分比计算器。
I try a simple calculation in the console (try it):
我在控制台中尝试了一个简单的计算(试试看):
106 / 100 * 10
And it returns me:
它返回给我:
10.600000000000001
What is going on here? Brackets makes no difference and this doesn't occur for every number. I would expect the result to be 10.6
right? Can anyone offer an explanation? This is not browser specific, it happens in Chrome dev tools and firebug.
这里发生了什么?括号没有区别,这不会出现在每个数字上。我希望结果是10.6
正确的?任何人都可以提供解释吗?这不是特定于浏览器的,它发生在 Chrome 开发工具和 firebug 中。
采纳答案by Denys Séguret
No, the result is correct enough (even if changing the order of operations could lead to a different result), that's one of the miracles of IEEE754 floating point storage : a consequences of the fact that numbers aren't stored like you see them, that is some decimal digits and a comma but as
不,结果足够正确(即使更改操作顺序可能会导致不同的结果),这是 IEEE754 浮点存储的奇迹之一:数字不像您看到的那样存储,那是一些十进制数字和一个逗号,但作为
K * 2 ^ N
where K and N are signed integers.
其中 K 和 N 是有符号整数。
As of course not all numbers can be stored exactly, others are only approached.
当然,并非所有数字都可以准确存储,其他数字只能接近。
I'd suggest you to read this introduction to IEEE754 computing.
我建议您阅读IEEE754 计算简介。
What you need is to format the number when outputting it to the user, for example with
您需要的是在将数字输出给用户时对其进行格式化,例如
var string = myNumber.toFixed(1);
回答by ckersch
Not all decimal numbers have exact representations in binary, so problems like this happen pretty regularly when you try to do math with floats.
并非所有的十进制数都有精确的二进制表示,因此当您尝试使用浮点数进行数学运算时,此类问题经常发生。
Converted to binary, your operation is as follows:
转换成二进制,你的操作如下:
106 = 64 + 32 + 8 + 2 = 1101010
100 = 64 + 32 + 4 = 1100100
1101010 / 1100100 = 1010.10011001100...
You then multiply this by 10, or 101 in binary and get
然后将其乘以 10 或二进制的 101 并得到
1010.10011001100... * 101 = 1010.1001100110011001100...
Which does not quiteevaluate to 10.6.
这并不完全评估为 10.6。
In your particular case, the number has been truncated at 1010.10011001100110011001100110011001, or 36 bits.
在您的特定情况下,该数字已在 1010.10011001100110011001100110011001 或 36 位处被截断。
回答by Kishan Patel
Try Below :
尝试以下:
Var myAnswer = 106 / 100 * 10;
var result = myAnswer.toFixed(1);
Javascript: formatting a rounded number to N decimals
How this will work for you...
这将如何为您工作...
Enajoy your day..!!!
享受你的一天..!!!
回答by code_cookies
you could try (106 / 1000) * 100
你可以试试 (106 / 1000) * 100
回答by Jason
Double precision. Numbers are stored in the computer as powers of two, e.g. 1/2 + 1/4 + 1/8...
so when printed as decimals they may not be exact.
双精度。数字以 2 的幂的形式存储在计算机中,例如1/2 + 1/4 + 1/8...
,当打印为小数时,它们可能不准确。