Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two percentage strings

Tags:

javascript

I have two strings: 50% and 60%. I'm trying to compare the two strings and figure out that 60% is bigger than 50%. What would be the easiest and most clean way to compare the two strings? I can always remove the % sign, convert the strings to ints and compare them. But is there a better way?

like image 492
vesii Avatar asked Sep 10 '25 03:09

vesii


1 Answers

You can use parseInt to parse only the numeric part.

let x = "50%";
let y = "60%";
parseInt(x)>parseInt(y)?console.log(`${x} is greater than ${y}`):console.log(`${y} is greater than ${x}`);

Console: 60% is greater than 50%
like image 114
Adil Ahmed Avatar answered Sep 12 '25 16:09

Adil Ahmed