Transform pseudo currency to number in JavaScript

In JavaScript I want to change some formatted numbers to a regular number.

The code might be look like that:

var aNb=[ 1500, "1,500", "15,00", "1,500,000", "1,500,000.25", "1000.25", "1 000 000"];
for (var i=0; i < aNb.length; i++) {
  var nb = aNb[i] + "";
  if (nb.split(",").length <= 2)
    nb = nb.replace(/,(\d{2})$/,".$1");
  nb = nb.replace(/[^\d\.\-]/g, '').replace(/\s/g,"")
  
  console.log(aNb[i], "=>", nb);
}

/* result:
1500 => 1500
1,500 => 1500
15,00 => 15.00
1,500,000 => 1500000
1,500,000.25 => 1500000.25
1000.25 => 1000.25
1 000 000 => 1000000
*/

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

*