javascript math get percentage 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: percentage formula in javascript

const result = Math.round((data.expense / data.income) * 100)