Javascript 如何在javascript中解析带有两位小数的浮点数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4435170/
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 parse float with two decimal places in javascript?
提问by user357034
I have the following code. I would like to have it such that if price_result equals an integer, let's say 10, then I would like to add two decimal places. So 10 would be 10.00. Or if it equals 10.6 would be 10.60. Not sure how to do this.
我有以下代码。我想要它,如果 price_result 等于一个整数,比如说 10,那么我想添加两个小数位。所以 10 就是 10.00。或者如果它等于 10.6 就是 10.60。不知道如何做到这一点。
price_result = parseFloat(test_var.split('$')[1].slice(0,-1));
回答by Mahesh Velaga
回答by Vlada
When you use toFixed
, it always returns the value as a string. This sometimes complicates the code. To avoid that, you can make an alternative method for Number.
当您使用 时toFixed
,它总是以字符串形式返回值。这有时会使代码复杂化。为避免这种情况,您可以为 Number 制作另一种方法。
Number.prototype.round = function(p) {
p = p || 10;
return parseFloat( this.toFixed(p) );
};
and use:
并使用:
var n = 22 / 7; // 3.142857142857143
n.round(3); // 3.143
or simply:
或者干脆:
(22/7).round(3); // 3.143
回答by Rob Boerman
If you need performance (like in games):
如果您需要性能(例如在游戏中):
Math.round(number * 100) / 100
It's about 100 times as fast as parseFloat(number.toFixed(2))
它大约是 parseFloat(number.toFixed(2)) 的 100 倍
回答by pvanallen
To return a number, add another layer of parentheses. Keeps it clean.
要返回数字,请添加另一层括号。保持清洁。
var twoPlacedFloat = parseFloat((10.02745).toFixed(2));
回答by Savage
If your objective is to parse, and your input might be a literal, then you'd expect a float
and toFixed
won't provide that, so here are two simple functions to provide this:
如果您的目标是解析,并且您的输入可能是文字,那么您会期望 afloat
并且toFixed
不会提供,所以这里有两个简单的函数来提供:
function parseFloat2Decimals(value) {
return parseFloat(parseFloat(value).toFixed(2));
}
function parseFloat2Decimals(value,decimalPlaces) {
return parseFloat(parseFloat(value).toFixed(decimalPlaces));
}
回答by Antonio Terreno
ceil from lodash is probably the best
来自 lodash 的 ceil 可能是最好的
_.ceil("315.9250488",2)
_.ceil(315.9250488,2)
_.ceil(undefined,2)
_.ceil(null,2)
_.ceil("",2)
will work also with a number and it's safe
也可以使用数字,而且很安全
回答by abhinav pandey
@sd Short Answer:There is no way in JS to have Number datatype value with trailing zeros after a decimal.
@sd 简短回答:JS 中没有办法让 Number 数据类型值在小数点后带有尾随零。
Long Answer:Its the property of toFixed
or toPrecision
function of JavaScript, to return the String. The reason for this is that the Number datatype cannot have value like a = 2.00, it will always remove the trailing zeros after the decimal, This is the inbuilt property of Number Datatype. So to achieve the above in JS we have 2 options
长答案:它是 JavaScript的属性toFixed
或toPrecision
功能,返回字符串。这样做的原因是 Number 数据类型不能有像 a = 2.00 这样的值,它总是会删除小数点后的尾随零,这是 Number Datatype 的内置属性。因此,要在 JS 中实现上述目标,我们有 2 个选项
回答by Rizo
You can use .toFixed() to for float value 2 digits
您可以使用 .toFixed() 来获取浮点值 2 位
Exampale
例子
let newValue = parseFloat(9.990000).toFixed(2) //output 9.99
let newValue = parseFloat(9.990000).toFixed(2) //output 9.99
回答by Nimesh
Please use below function if you don't want to round off.
如果您不想四舍五入,请使用以下功能。
function ConvertToDecimal(num) {
num = num.toString(); //If it's not already a String
num = num.slice(0, (num.indexOf(".")) + 3); //With 3 exposing the hundredths place
alert('M : ' + Number(num)); //If you need it back as a Number
}
回答by andora
For what its worth: A decimal number, is a decimal number, you either round it to some other value or not. Internally, it will approximate a decimal fraction according to the rule of floating point arthmetic and handling. It stays a decimal number (floating point, in JS a double) internally, no matter how you many digits you want to display it with.
对于它的价值:十进制数是十进制数,您可以将其四舍五入为其他值或不四舍五入。在内部,它会根据浮点运算和处理规则来近似一个小数部分。它在内部保持十进制数(浮点数,在 JS 中为双精度数),无论您想用多少位数显示它。
To present it for display, you can choose the precision of the display to whatever you want by string conversion. Presentation is a display issue, not a storage thing.
要将其呈现以供显示,您可以通过字符串转换将显示精度选择为您想要的任何值。演示是显示问题,而不是存储问题。