Skip to content

Instantly share code, notes, and snippets.

@rxaviers
Created January 26, 2015 17:28
Show Gist options
  • Save rxaviers/599b1888375894f8487d to your computer and use it in GitHub Desktop.
Save rxaviers/599b1888375894f8487d to your computer and use it in GitHub Desktop.
function decimalAdjust(type, value, increment) {
increment = increment || 1;
// If the exp is undefined or zero...
if (increment === undefined || increment === 1) {
return Math[type](value);
}
value = +value;
// If the value or increment is not a number...
if (isNaN(value) || isNaN(increment)) {
return NaN;
}
increment = increment.toExponential().split( "e" );
// Shift
value = value.toString().split('e');
value = Math[type](+(+value[0]/increment[0] + 'e' + (value[1] ? (+value[1] - increment[1]) : -increment[1])));
// Shift back
value = value.toString().split('e');
return +(+value[0]*increment[0] + 'e' + (value[1] ? (+value[1] + increment[1]) : increment[1]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment