JavaScript“parseInt()”十进制字符串的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28894971/
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
Problems with JavaScript "parseInt()" decimal string
提问by Funny Frontend
I get the total_amount in cart code of ecommerce web page, after I use toString function, but when I convert the 14.82 string to int number, the decimals disappear.
使用toString函数后,我在电子商务网页的购物车代码中获得了total_amount,但是当我将14.82字符串转换为int数字时,小数点消失了。
<script>
var total_amount_string = <?=json_encode($cart['total_pvp'])?>;//-> 14,82
var total_amount_int = parseInt(total_amount_string).toFixed(2); //-> 14.00(here is the error)
console.log(total_amount_string) //-> 14,82
console.log(total_amount_int) //-> 14.00
</script>
What's the matter?
怎么了?
回答by T.J. Crowder
If the input string is "14,82"and you want the value 14.82, you'll need to convert the ,to a .and then use parseFloat:
如果输入字符串是"14,82"并且您想要 value 14.82,则需要将 the 转换,为 a.然后使用parseFloat:
var total_amount_int = parseFloat(
total_amount_string.replace(/,/g, ".")
).toFixed(2);
parseIntwill only parse the leading part of the string that defines a whole number ("int" = "integer" = "whole number"), so it stops at the ,. parseFloatwill parse a decimal number, but only understands .as the decimal point, not ,, even in locales where ,is the decimal point.
parseInt只会解析定义整数的字符串的前导部分(“int”=“integer”=“whole number”),因此它在,. parseFloat将解析一个十进制数,但只能理解.为小数点,而不是,,即使在,小数点所在的语言环境中也是如此。
回答by T.J. Crowder
you should use parseFloat() instead of parseInt(). Because integer number is not decimal.
您应该使用 parseFloat() 而不是 parseInt()。因为整数不是十进制。
回答by Pablo Lozano
And integer has no decimal part, so any number "casted" to int will lose its decimals. You should use parseFloatif you want to keep the decimal part.
On the other hand, make sure you are not using a comma: Javascript parse float is ignoring the decimals after my comma
整数没有小数部分,因此任何“转换”为 int 的数字都将丢失小数。parseFloat如果要保留小数部分,则应使用。另一方面,请确保您没有使用逗号:Javascript parse float 忽略逗号后的小数
回答by Staale
Your number is using a "," separator. Unfortunately there is no locale settings for number parsing in JavaScript, so you are forced to a bit more hacky:
您的号码使用了“,”分隔符。不幸的是,在 JavaScript 中没有用于数字解析的语言环境设置,因此您不得不更加笨拙:
var total_amount_string = "14,823462";
var total_amount_float = parseFloat(total_amount_string.replace(",", ".")).toFixed(2);
document.getElementById("total_amount_string").innerText = total_amount_string;
document.getElementById("total_amount_float").innerText = total_amount_float;
total_amount_string: <span id="total_amount_string"></span>
<br />
total_amount_float: <span id="total_amount_float"></span>
回答by Wintergreen
The parseInt() function parses a string and returns an integer. ParseInt returns only the first number in the string is returned!. If the first character cannot be converted to a number, parseInt() returns NaN.
parseInt() 函数解析一个字符串并返回一个整数。ParseInt 只返回返回字符串中的第一个数字!。如果第一个字符无法转换为数字,则 parseInt() 返回 NaN。

