Skip to content

Instantly share code, notes, and snippets.

@eeeps
Created July 29, 2021 18:40
Show Gist options
  • Save eeeps/75a31a2e1815b8bd4bf621de890f47e4 to your computer and use it in GitHub Desktop.
Save eeeps/75a31a2e1815b8bd4bf621de890f47e4 to your computer and use it in GitHub Desktop.
roundToEvenIfEquidistant.js
function roundToEvenIfEquidistant( float ) {
// equidistant case
if ( Math.ceil( float ) - float === float - Math.floor( float ) ) {
if ( Math.ceil( float ) % 2 === 0 ) { // if ceil is even
return Math.ceil( float );
} else {
return Math.floor( float );
}
} else {
// non-equidistant
return Math.round( float );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment