Carol.Kar
Carol.Kar

Reputation: 5345

Convert stringified numbers back to number

I am having numbers that are stringified, such as ["84.0711 billion", "$52.6138 billion", "$43.55 billion", "$54.73 million"]. I would like to convert these numbers back to: ["84071100000", "52613800000", "43550000000", "54730000"]

let numb = ["84.0711 billion", "$52.6138 billion", "$43.55 billion", "$54.73 million"]
const res = []

for (let i = 0; i < numb.length; i++) {
  res.push(convertNumber(numb[i]))
}
console.log(JSON.stringify(res));
//wanted output: ["84071100000", "52613800000", "43550000000", "54730000"]


function convertNumber(numb) {
  var digits = numb.match(/\d+/g).map(Number);
  if (numb.match('billion')) digits + 1000000000
  if (numb.match('million')) digits + 1000000
  return digits
}

I tried to get only the digits and then add the respective notation. However, I only get the following output.(see above my example)

Any suggestions what I am doing wrong?

Upvotes: 3

Views: 539

Answers (4)

baao
baao

Reputation: 73221

You could use a single regex

let numb = ["84.0711 billion", "$52.6138 billion", "$43.55 billion", "$54.73 million"]

const conversion = {
    billion: 1000000000,
    million: 1000000
};

function convert(entry) {
    return entry.replace(/^\$?(\S+)\s?(\w+)/g, (a, n, e) => {
        return Number(n) * conversion[e];
    })
}

console.log(convert(numb[0]));

Upvotes: 4

Nina Scholz
Nina Scholz

Reputation: 386550

You need to take the dot and decimals after the dot as well and multiply the value and assign the result to digits.

Then you could seach for the wanted factor and take a default value for multiplying.

function convert(string) {
    var factors = { million: 1e6, billion: 1e9 },
        value = string.match(/\d+\.?\d*/g),
        factor = string.match(new RegExp(Object.keys(factors).join('|'), 'i'));

    return (value || 0) * (factors[factor] || 1);
}

var strings = ["84.0711 billion", "$52.6138 billion", "$43.55 billion", "$54.73 million"],
    result = strings.map(convert);

console.log(result); // [84071100000, 52613800000, 43550000000, 54730000]

Upvotes: 1

VLAZ
VLAZ

Reputation: 28970

You were pretty close, actually but there are few minor issues:

  • the regex /\d+/g will fail you because it only finds digits but you also have . as a decimal separator. So you need to account for it in the pattern - /\d+(?:\.\d+)?/g - it's any number of digits, optionally followed by a literal dot and then any number of digits again.
    • the regex result gives you an array with all the matches. You only want the number, so you can get it by doing [0]
    • if (numb.match('billion')) digits + 1000000000 you actually want to multiply, not add. Because 1.21 thousand is 1.21 * 1000 = 1210 not 1.21 + 1000 = 1001.21
    • you need to assign the value back to the digits - digits * 1000000000 will perform the operation but will then discard the result - if you do digits = digits * 1000000000 then it will be saved. You can also write that as digits *= 1000000000 - multiply the value and assign it back.
    • instead of matches use includes. It's a bit simpler to use - it will check if the string has the value you want, while matches will try to run it against a regex, which is a bit less predictable if you have, say, . or brackets.

let numb = ["84.0711 billion", "$52.6138 billion", "$43.55 billion", "$54.73 million"]
const res = []

for (let i = 0; i < numb.length; i++) {
  res.push(convertNumber(numb[i]))
}
console.log(JSON.stringify(res));
//wanted output: ["84071100000", "52613800000", "43550000000", "54730000"]


function convertNumber(numb) {
  var digits = numb
    .match(/\d+(?:\.\d+)/g)
    .map(Number)[0];
    
  if (numb.includes('billion')) digits *= 1000000000
  if (numb.includes('million')) digits *= 1000000
  return digits
}

Upvotes: 1

User863
User863

Reputation: 20039

Using replace()

replace(/[^\d.]/g, '') - remove everything except numbers and dot

+numb - unary plus converts string to Number

let numb = ["84.0711 billion", "$52.6138 billion", "$43.55 billion", "$54.73 million"]
const res = []

for (let i = 0; i < numb.length; i++) {
  res.push(convertNumber(numb[i]))
}
console.log(JSON.stringify(res));

function convertNumber(numb) {
  var digits = +numb.replace(/[^\d.]/g, '');
  if (numb.match('billion')) digits = digits * 1000000000
  if (numb.match('million')) digits = digits * 1000000
  return digits
}

Upvotes: 1

Related Questions