Created
April 20, 2012 14:58
-
-
Save taylorkearns/2429316 to your computer and use it in GitHub Desktop.
Rounds decimals ending in .49 up to .5. (Native JS Math.round() method rounds XX.49 down to XX.) If you want to use the Math.round() method but want to round up X.49, use this method in place of Math.round().
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
var _round = function(num) { | |
if(isDecimal(num)) { | |
var num_array = num.toString().split("."); | |
var whole_num = parseInt(num_array[0]); | |
var decimal_num = parseFloat("." + num_array[1]); | |
if(decimal_num > .444444444444444 && decimal_num < .5) { | |
decimal_num = .5; | |
} | |
num = whole_num + decimal_num; | |
} | |
return Math.round(num); | |
}; | |
var isDecimal = function(num) { | |
return num % 1 === 0 ? false : true; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment