Make a number a percentage

user1040259 picture user1040259 · Dec 15, 2011 · Viewed 236.3k times · Source

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) + "%");
});

Answer

Naftali aka Neal picture Naftali aka Neal · Dec 15, 2011

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!