Reputation: 5345
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
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
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
Reputation: 28970
You were pretty close, actually but there are few minor issues:
/\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.
[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
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.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
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