Code coverage report for number/enforcePrecision.js

Statements: 100% (6 / 6)      Branches: 100% (0 / 0)      Functions: 100% (3 / 3)      Lines: 100% (5 / 5)     

All files » number/ » enforcePrecision.js
1 2 3 4 5 6 7 8 9 10 11 12 13 141             1 144 144   1    
define(function(){
    /**
     * Enforce a specific amount of decimal digits and also fix floating
     * point rounding issues.
     * @example `enforcePrecision(0.615, 2) -> 0.62`, `(0.615).toFixed(2) ->
     * 0.61`
     */
    function enforcePrecision(val, nDecimalDigits){
        var pow = Math.pow(10, nDecimalDigits);
        return +(Math.round(val * pow) / pow).toFixed(nDecimalDigits);
    }
    return enforcePrecision;
});