Was using ExtJS for formatting numbers to percentage earlier. Now as we are not using ExtJS anymore same has to be accomplished using normal JavaScript.
Need a method that will take number and format (usually in %) and convert that number to that format.
0 -> 0.00% = 0.00% 25 -> 0.00% = 25.00% 50 -> 0.00% = 50.00% 150 -> 0.00% = 150.00%
Make a Number a Percentage with JavaScript with Arithmetic const number1 = 4.954848; const number2 = 5.9797; console. log(Math. floor((number1 / number2) * 100)); to get the percentage of number1 of number2 by dividing number1 by number2 .
Percentages are calculated by using the equation amount / total = percentage. For example, if a cell contains the formula =10/100, the result of that calculation is 0.1. If you then format 0.1 as a percentage, the number will be correctly displayed as 10%.
floor((number1 / number2) * 100)); //w00t! alert(~~((number1 / number2) * 100)); as the Math.
JavaScript numbers can be formatted in different ways like commas, currency, etc. You can use the toFixed() method to format the number with decimal points, and the toLocaleString() method to format the number with commas and Intl. NumberFormat() method to format the number with currency.
Here is what you need.
var x=150; console.log(parseFloat(x).toFixed(2)+"%"); x=0; console.log(parseFloat(x).toFixed(2)+"%"); x=10 console.log(parseFloat(x).toFixed(2)+"%");
You can use Number.toLocaleString():
var num=25; var s = Number(num/100).toLocaleString(undefined,{style: 'percent', minimumFractionDigits:2}); console.log(s);
25.00%
See documentation for toLocaleString() for more options for the format object parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With