Skip to content
Advertisement

Make a number a percentage

What’s the best way to strip the “0.”XXX% off a number and make it a percentage? What happens if the number happens to be an int?

var number1 = 4.954848;
var number2 = 5.9797;

$(document).ready(function() {    
    final = number1/number2;
    alert(final.toFixed(2) + "%");
});

Advertisement

Answer

A percentage is just:

(number_one / number_two) * 100

No need for anything fancy:

var number1 = 4.954848;
var number2 = 5.9797;

alert(Math.floor((number1 / number2) * 100)); //w00t!
Advertisement