Problems with JavaScript "parseInt()" decimal string

you should use parseFloat() instead of parseInt(). Because integer number is not decimal.


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:

var total_amount_int = parseFloat(
        total_amount_string.replace(/,/g, ".")
    ).toFixed(2);

parseInt will only parse the leading part of the string that defines a whole number ("int" = "integer" = "whole number"), so it stops at the ,. parseFloat will parse a decimal number, but only understands . as the decimal point, not ,, even in locales where , is the decimal point.


An integer has no decimal part, so any number "casted" to int will lose its decimals. You should use parseFloat if 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

Tags:

Javascript