How to format a number as a percent in Node.js

3 Answers

0 votes
function formatNumberAsPercentage(num) {
    return new Intl.NumberFormat('default', {
            style: 'percent',
            minimumFractionDigits: 2,
            maximumFractionDigits: 2,
    }).format(num / 100);
}
 
console.log(formatNumberAsPercentage(83.1)); 
console.log(formatNumberAsPercentage(98)); 
 
 
 
   
   
   
/*
run:
   
83.10%
98.00%
   
*/

 



answered Apr 19, 2022 by avibootz
0 votes
let n = 87.1;
console.log(parseFloat(n).toFixed(2) + "%");
 
n = 40;
console.log(parseFloat(n).toFixed(2) + "%");
 
n = 0;
console.log(parseFloat(n).toFixed(2) + "%");
 
 
 
   
   
   
/*
run:
   
87.10%
40.00%
0.00%
   
*/

 



answered Apr 19, 2022 by avibootz
0 votes
function formatNumberAsPercentage(num) {
    return Number(num / 100).toLocaleString(undefined,
                  {style: 'percent', minimumFractionDigits:1}); 
}
 
console.log(formatNumberAsPercentage(51.8)); 
console.log(formatNumberAsPercentage(16)); 
 
 
 
   
   
   
/*
run:
   
51.8%
16.0%

*/

 



answered Apr 19, 2022 by avibootz

Related questions

1 answer 152 views
1 answer 245 views
3 answers 189 views
3 answers 182 views
1 answer 182 views
1 answer 99 views
1 answer 148 views
...