Javascript javascript将int转换为float
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4057489/
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
javascript convert int to float
提问by Rajesh
I have a variable
我有一个变量
var fval = 4;
now I want out put as 4.00
现在我想输出为 4.00
回答by Alex Jasmin
JavaScript only has a Numbertype that stores floating point values.
JavaScript 只有一个Number类型来存储浮点值。
There is no int.
没有整数。
Edit:
编辑:
If you want to format the number as a string with two digits after the decimal point use:
如果要将数字格式化为小数点后两位数的字符串,请使用:
(4).toFixed(2)
回答by nkshio
toFixed()method formats a number using fixed-point notation. Read MDN Web Docsfor full reference.
toFixed()方法使用定点表示法格式化数字。阅读MDN Web 文档以获取完整参考。
var fval = 4;
console.log(fval.toFixed(2)); // prints 4.00