Sha256: 921a8eeced4d65ddabe60883e9025b282dacf0c2468b3d97345fd770364618fe

Contents?: true

Size: 1.93 KB

Versions: 24

Compression:

Stored size: 1.93 KB

Contents

// ==========================================================================
// Project:   SproutCore - JavaScript Application Framework
// Copyright: ©2006-2011 Strobe Inc. and contributors.
//            Portions ©2008-2011 Apple Inc. All rights reserved.
// License:   Licensed under MIT license (see license.js)
// ==========================================================================

/**
  @class
  
  Implements some enhancements to the built-in Number object that makes it
  easier to handle rounding and display of numbers.
  
  @since SproutCore 1.0
  @author Colin Campbell
*/
SC.Math = SC.Object.create(
/** @lends SC.Math.prototype */ {
  
  /**
    Checks to see if the number is near the supplied parameter to a certain lambda.
    
    @param {Number} n1 First number in comparison.
    @param {Number} n2 Number to compare against the first.
    @param {Number} lambda The closeness sufficient for a positive result. Default 0.00001
    @returns {Boolean}
  */
  near: function(n1, n2, lambda) {
    if (!lambda) lambda = 0.00001;
    return Math.abs(n1 - n2) <= lambda;
  },
  
  /**
    Rounds a number to a given decimal place. If a negative decimalPlace
    parameter is provided, the number will be rounded outward (ie. providing
    -3 will round to the thousands).
    
    Function is insufficient for high negative values of decimalPlace parameter.
    For example, (123456.789).round(-5) should evaluate to 100000 but instead
    evaluates to 99999.999... 
    
    @param {Number} n The number to round
    @param {Integer} decimalPlace
    @returns {Number}
  */
  round: function(n, decimalPlace) {
    if (!decimalPlace) decimalPlace = 0;
    var factor = Math.pow(10, decimalPlace);
    if (decimalPlace < 0) {
       // stop rounding errors from hurting the factor...
      var s = factor.toString();
      factor = s.substring(0, s.indexOf("1")+1);
    }
    n = n.valueOf();
    return Math.round(n * factor) / factor;
  }
  
}) ;

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
sproutcore-1.11.0 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.11.0.rc3 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.11.0.rc2 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.11.0.rc1 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.10.3.1 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.10.2 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.10.1 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.10.0 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.10.0.rc.3 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.10.0.rc.2 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.10.0.rc.1 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.9.2 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.9.1 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.9.0 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.8.2.1 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.8.1 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.8.0 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.7.1.beta-java lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.7.1.beta lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.6.0.1-java lib/frameworks/sproutcore/frameworks/foundation/system/math.js