Skip to content

Instantly share code, notes, and snippets.

@chetanraj
Created May 25, 2016 11:58
Show Gist options
  • Save chetanraj/fe3f6be3299a72ab6672c84e212a63f6 to your computer and use it in GitHub Desktop.
Save chetanraj/fe3f6be3299a72ab6672c84e212a63f6 to your computer and use it in GitHub Desktop.
Percentage Calculation - Rounding off to Single Decimal Point
// Require Underscore.js
Percentage = function(numbers) {
var percentage = [],
sum = _.reduce(numbers, function(m, number){ return m + number; }, 0);
for(var n = 0; n < numbers.length; n++){
percentage[n] = parseFloat((numbers[n]/sum * 100).toFixed(3));
percentage[n] = Math.round(percentage[n] * 10)/10;
};
return percentage;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment