convert number to percentage javascript code example

Example 1: get percentage of number javascript

//Our number.
var number = 120;
 
//The percent that we want to get.
//i.e. We want to get 50% of 120.
var percentToGet = 50;
 
//Calculate the percent.
var percent = (percentToGet / 100) * number;
 
//Alert it out for demonstration purposes.
alert(percentToGet + "% of " + number + " is " + percent);
 
//The result: 50% of 120 is 60

Example 2: how to turn decimales into percents with javascript

var obtained = 475;var total = 500;var percent = obtained*100/total;console.log(percent); // 95.0