How To Convert String Equation To Number In Javascript?
Solution 1:
If you're sure that the string will always be something like "100*100" you could eval()
it, although most people will tell you this isn't a good idea on account of the fact that people could pass in malicious code to be eval'd.
eval("100*100");
>>10000
Otherwise, you'll have to find or write a custom equation parser. In that case, you might want to take a look at the Shunting-yard algorithm, and read up on parsing.
Using split()
:
var myEquation = "100*100";
var num = myEquation.split("*")[0] * myEquation.split("*")[1];
>> 10000
Solution 2:
This will give you the product whether the string is using *
or x
:
var str = "100x100";
var tmp_arr = str.split(/[*x]/);
var product = tmp_arr[0]*tmp_arr[1]; // 10000
Solution 3:
use parseInt() ,The parseInt() function parses a string and returns an integer.
parseInt("100")*parseInt("100") //10000
Solution 4:
One Liner:
str.split(/[x*]/).reduce((a,b) => a*b)
Solution 5:
I have a different take on the users answers here that gives an output using split but also recreates the original equation. The other answers I saw used split but didn't really give a correlation that could be parameterized of the original equation in starting out as string format.
Using split()
and for
is NOT a good solution but I use it here to illustrate the better solution that doesn't require knowing the number of numbers in the array (which would be reduce). The reason why this works is that in effect it acts like reduce but it requires you to edit the for loop multiplier based on how large the array is:
let someEnvironmentVariable = '3*3*3'let equationString = someEnvironmentVariable;
// regex to match anything in the set i.e. * and + << not sure if need the ending +let re = /[\*\+]+/;
let equationToNumberArray = equationString.split(re).map(Number);
let equationResolver = 0;
for (const numericItem of equationToNumberArray) {
// recreate the equation above
equationResolver += numericItem*numericItem;
}
console.log(equationResolver);
A more elegant solution would be to use:
split()
, map()
and reduce()
let someEnvironmentVariable = '3*3*4*3'let equationString = someEnvironmentVariable;
// regex to match anything in the set i.e. * and + << not sure if need the ending +let re = /[\*\+]+/;
let equationToNumberArray = equationString.split(re).map(Number);
let arrayMultiplier = equationToNumberArray.reduce((a, b) => a * b);
console.log(arrayMultiplier);
Using reduce allows you to iterate through the array and perform a calculation on each item while maintaining the previous calculation on the previous item.
Note: what I don't like about either solution is that even reduce requires only one mathematical operation for an array set. A better solution would be code something that detects the operators and includes that in the overall solution
This allows you to not use the dreaded eval()
but also keep the original equation intact for later processing.
Post a Comment for "How To Convert String Equation To Number In Javascript?"