Skip to content Skip to sidebar Skip to footer

ParseInt Fixing The Value

Here I have a value with commas. Ex:-var abc = '10,10.12'; If I use var x = parseInt(abc); it is returning 10 Expected output is 10,10.12 as I have used ng-value='UpdatedPropuesta.

Solution 1:

If you want an array of numbers out of the string then try this,

const input = "10,10.12";
var output = input.split(",");
output = output.map(i => Number(i));
console.log(output);

Solution 2:

 10,10.12

That is not the number 1010.12, it is the number 10, a comma operator, and the number 10.12.

Commas are not part of the JavaScript literal syntax.

However, in your case you're passing two arguments to parseInt, the first should be a string to convert (but JS will convert it to a strign) and the second is the radix – the number base – which should be an integer.

So JS's type conversion will lead to:

var x = parseInt('10', 10);

Which is of course 10.

After question update

var x = parseInt("10,10.12");

As comma are not part of JS numeric literals, the parse will stop at the comma because it is not a character that can appear in a number.

So the answer is still 10.


Post a Comment for "ParseInt Fixing The Value"