Created
May 25, 2016 11:58
-
-
Save chetanraj/fe3f6be3299a72ab6672c84e212a63f6 to your computer and use it in GitHub Desktop.
Percentage Calculation - Rounding off to Single Decimal Point
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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