round.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.math.round"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.math.round"] = true;
  8. dojo.provide("dojox.math.round");
  9. dojo.getObject("math.round", true, dojox);
  10. dojo.experimental("dojox.math.round");
  11. dojox.math.round = function(/*Number*/value, /*Number?*/places, /*Number?*/increment){
  12. // summary:
  13. // Similar to dojo.number.round, but compensates for binary floating point artifacts
  14. // description:
  15. // Rounds to the nearest value with the given number of decimal places, away from zero if equal,
  16. // similar to Number.toFixed(). Rounding can be done by fractional increments also.
  17. // Makes minor adjustments to accommodate for precision errors due to binary floating point representation
  18. // of Javascript Numbers. See http://speleotrove.com/decimal/decifaq.html for more information.
  19. // Because of this adjustment, the rounding may not be mathematically correct for full precision
  20. // floating point values. The calculations assume 14 significant figures, so the accuracy will
  21. // be limited to a certain number of decimal places preserved will vary with the magnitude of
  22. // the input. This is not a substitute for decimal arithmetic.
  23. // value:
  24. // The number to round
  25. // places:
  26. // The number of decimal places where rounding takes place. Defaults to 0 for whole rounding.
  27. // Must be non-negative.
  28. // increment:
  29. // Rounds next place to nearest value of increment/10. 10 by default.
  30. // example:
  31. // >>> 4.8-(1.1+2.2)
  32. // 1.4999999999999996
  33. // >>> Math.round(4.8-(1.1+2.2))
  34. // 1
  35. // >>> dojox.math.round(4.8-(1.1+2.2))
  36. // 2
  37. // >>> ((4.8-(1.1+2.2))/100)
  38. // 0.014999999999999996
  39. // >>> ((4.8-(1.1+2.2))/100).toFixed(2)
  40. // "0.01"
  41. // >>> dojox.math.round((4.8-(1.1+2.2))/100,2)
  42. // 0.02
  43. // >>> dojox.math.round(10.71, 0, 2.5)
  44. // 10.75
  45. // >>> dojo.number.round(162.295, 2)
  46. // 162.29
  47. // >>> dojox.math.round(162.295, 2)
  48. // 162.3
  49. var wholeFigs = Math.log(Math.abs(value))/Math.log(10);
  50. var factor = 10 / (increment || 10);
  51. var delta = Math.pow(10, -15 + wholeFigs);
  52. return (factor * (+value + (value > 0 ? delta : -delta))).toFixed(places) / factor; // Number
  53. }
  54. if((0.9).toFixed() == 0){
  55. // (isIE) toFixed() bug workaround: Rounding fails on IE when most significant digit
  56. // is just after the rounding place and is >=5
  57. (function(){
  58. var round = dojox.math.round;
  59. dojox.math.round = function(v, p, m){
  60. var d = Math.pow(10, -p || 0), a = Math.abs(v);
  61. if(!v || a >= d || a * Math.pow(10, p + 1) < 5){
  62. d = 0;
  63. }
  64. return round(v, p, m) + (v > 0 ? d : -d);
  65. }
  66. })();
  67. }
  68. }