-1

I have multiple strings that looks like these: 550 e, 1,550 e, 1.550,00 e I need to convert it to numbers so I can compare its values.

I removed currency part (e) w/ split() and then join() but how to get numbers from that?

dbz
  • 411
  • 7
  • 22
youngster
  • 195
  • 1
  • 12

2 Answers2

0

Try using something like :

var integer = parseInt(yourString, 10);

for decimal numbers :

 var decimal= parseFloat(yourString);
Kevin.a
  • 4,094
  • 8
  • 46
  • 82
-1

use Regular Expression to capture digits followed by optional decimal.

var regex = /\d+.?\d+/g;

const arrofStrings = ["550 e", "1,550 e", "1.550,00 e"];

const numbers = arrofStrings.map((str) => {
    str = str.replace(',', '');
    return Number(str.match(regex));
});

console.log(numbers);
random
  • 7,756
  • 3
  • 19
  • 25
  • almost, note that 1,550 and 1.550,00 are same, they both should be, for example, 1550,00 – youngster Jun 12 '19 at 13:42
  • When running this code base, 1.550,00 should return 1550, not 1.55, so you would need to account for that – Fallenreaper Jun 12 '19 at 16:31
  • I solved this with: `arrofStrings.map( item => parseInt(item.split(" ")[0].replace(".","").split(",")[0]) )`, though this is resolving for ints only, and not accounting for floats. – Fallenreaper Jun 12 '19 at 16:34