Created
January 26, 2015 17:28
-
-
Save rxaviers/599b1888375894f8487d to your computer and use it in GitHub Desktop.
decimalAdjust (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round) that uses increment instead
This file contains hidden or 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
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