Ben
Ben

Reputation: 1

Javascript eval()Javascript work with the percentage symbol (%) as a percentage but not as a reminder

How do I make the eval() in Javascript work with the percentage symbol (%) as a percentage but not as a reminder

I want to be able to let the eval() work with percentages as 1/100 not as a reminder

Upvotes: 0

Views: 487

Answers (2)

ali zaidi
ali zaidi

Reputation: 1

To check 3 percent of 200 I used * 0.01 *,

console.log(eval(3* 0.01 * 200));

Upvotes: -1

Dimava
Dimava

Reputation: 10899

As percentage is defined as * 0.01,

function evalWithPercentage(s) {
  return eval(s.replaceAll('%', ' * 0.01'))
}
console.log( evalWithPercentage('10% + 150%') )
//> 1.6

Upvotes: 0

Related Questions