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) + "%");
});
Solution 1:
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!
Solution 2:
((portion/total) * 100).toFixed(2) + '%'
Solution 3:
The best solution, where en
is the English locale:
fraction.toLocaleString("en", {style: "percent"})