Sha256: b1b3be93f8ffa03557adaf9e44450c134229662d310ba7ff5d9100b803c19425

Contents?: true

Size: 1.9 KB

Versions: 17

Compression:

Stored size: 1.9 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({
  
  /**
    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

17 entries across 17 versions & 1 rubygems

Version Path
sproutcore-1.6.0.rc.2-x86-mingw32 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.6.0.rc.2 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.6.0.rc.2-java lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.6.0.rc.1-x86-mingw32 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.6.0.rc.1 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.6.0.rc.1-java lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.6.0.beta.3-x86-mingw32 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.6.0.beta.3-java lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.6.0.beta.3 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.6.0.beta.2-x86-mingw32 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.6.0.beta.2-java lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.6.0.beta.2 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.6.0.beta.1 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.5.0-java lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.5.0 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.5.0.rc.2 lib/frameworks/sproutcore/frameworks/foundation/system/math.js
sproutcore-1.5.0.rc.1 lib/frameworks/sproutcore/frameworks/foundation/system/math.js