Skip to content

Instantly share code, notes, and snippets.

@taylorkearns
Created April 20, 2012 14:58
Show Gist options
  • Save taylorkearns/2429316 to your computer and use it in GitHub Desktop.
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().
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