Make a number a percentage
The best solution, where en
is the English locale:
fraction.toLocaleString("en", {style: "percent"})
Well, if you have a number like 0.123456
that is the result of a division to give a percentage, multiply it by 100 and then either round it or use toFixed
like in your example.
Math.round(0.123456 * 100) //12
Here is a jQuery plugin to do that:
jQuery.extend({
percentage: function(a, b) {
return Math.round((a / b) * 100);
}
});
Usage:
alert($.percentage(6, 10));
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!
((portion/total) * 100).toFixed(2) + '%'