Skip to content

Instantly share code, notes, and snippets.

@th3d0g
Created January 22, 2019 00:20
Show Gist options
  • Select an option

  • Save th3d0g/6fcbb04af035f021617d2de2a184e4ed to your computer and use it in GitHub Desktop.

Select an option

Save th3d0g/6fcbb04af035f021617d2de2a184e4ed to your computer and use it in GitHub Desktop.
Created with Copy to Gist
function roundToHalf(value) {
   var converted = parseFloat(value); // Make sure we have a number
   var decimal = (converted - parseInt(converted, 10));
   decimal = Math.round(decimal * 10);
   if (decimal == 5) { return (parseInt(converted, 10)+0.5); }
   if ( (decimal < 3) || (decimal > 7) ) {
      return Math.round(converted);
   } else {
      return (parseInt(converted, 10)+0.5);
   }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment