Created
March 1, 2012 09:53
-
-
Save msenkpiel/1948629 to your computer and use it in GitHub Desktop.
Javascript Utilities
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
var Utils = { | |
Math:{ | |
roundFloat:function(number, dec) { | |
if (dec < 1 || dec > 14) { | |
return false; | |
} | |
var e = Math.pow(10, dec); | |
var k = (Math.round(number * e) / e).toString(); | |
if (k.indexOf('.') == -1) { | |
k += '.'; | |
} | |
k += e.toString().substring(1); | |
return k.substring(0, k.indexOf('.') + dec + 1); | |
}, | |
getPercentOfRange:function(xValue, scaleMin, scaleMax){ | |
var len = (scaleMax - scaleMin); | |
return (xValue - scaleMin) / len; | |
}, | |
getPositionOfRange:function(xPercent, scaleMin, scaleMax){ | |
var len = (scaleMin - scaleMax); | |
return (xPercent * len) + scaleMin; | |
}, | |
transformValueToScale:function(xValue, sourceScaleMin, sourceScaleMax, targetScaleMin, targetScaleMax){ | |
return this.getPositionOfRange(this.getPercentOfRange(xValue, sourceScaleMin, sourceScaleMax), targetScaleMin, targetScaleMax); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment