Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting formatted string to integer in Javascript

how can i convert "570.581,88" into an integer and sort accordingly?

like image 736
Joseph Avatar asked Dec 09 '10 05:12

Joseph


People also ask

How do you convert a string to an integer in JavaScript?

In JavaScript parseInt() function (or a method) is used to convert the passed in string parameter or value to an integer value itself. This function returns an integer of base which is specified in second argument of parseInt() function.

What are the different methods by which we can convert a string to a number in JavaScript?

Converting Variables to Numbers There are 3 JavaScript methods that can be used to convert variables to numbers: The Number() method. The parseInt() method. The parseFloat() method.

What is parseInt in JavaScript?

Description. The parseInt function converts its first argument to a string, parses that string, then returns an integer or NaN . If not NaN , the return value will be the integer that is the first argument taken as a number in the specified radix .


1 Answers

var s = "570.581,88";

// Format as American input
s = s.replace(/\./g,'').replace(',','.');

// Integer
var i = parseInt(s,10);

// Floats
var f1 = parseFloat(s);
var f2 = s*1;
var f3 = +s;
like image 194
Phrogz Avatar answered Oct 05 '22 19:10

Phrogz