Javascript 如何将整数转换为双数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7312693/
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
How to Convert Int number to Double number?
提问by blackriderws
Using Javascript:
使用 JavaScript:
How I can convert one number integer to Double Number Example:
如何将一个数字整数转换为双数示例:
100 -> 100.00
回答by
All numbers in JavaScript aredoubles: that is, they are stored as 64-bit IEEE-754 doubles.
JavaScript中的所有数字都是双精度数:也就是说,它们存储为 64 位 IEEE-754 双精度数。
That is, the goal is not to get a "double": the goal is to get the string reprsentation of a number formatted as "YYY.XX". For that, consider Number.toFixed
, for instance:
也就是说,目标不是获得“双倍”:目标是获得格式为 "YYY.XX" 的数字的字符串表示。为此,请考虑Number.toFixed
,例如:
(100).toFixed(2)
The result is the string(not a "double"!) "100.00"
. The parenthesis are required to avoid a grammar ambiguity in this case (it could also have been written as 100.0.toFixed
or 100..toFixed
), but would not be required if 100 was in a variable.
结果是字符串(不是“double”!)"100.00"
。在这种情况下需要括号来避免语法歧义(它也可以写为100.0.toFixed
或100..toFixed
),但如果 100 在变量中,则不需要括号。
Happy coding.
快乐编码。
回答by Felix Kling
JavaScript numbers are already IEEE 754 floating point numbers(specification). If you want to formatit (for output), you can use toFixed
[MDN].
JavaScript 数字已经是IEEE 754 浮点数(规范)。如果要对其进行格式化(用于输出),可以使用toFixed
[MDN]。
回答by Eliu
All numbers are stored as doubles in javascript.
所有数字都以双精度形式存储在 javascript 中。
回答by VIKAS KOHLI
All numbers are stored as doubles in javascript. You can use toFixed if you want specific decimal points after the number
所有数字都以双精度形式存储在 javascript 中。如果你想在数字后使用特定的小数点,你可以使用 toFixed