round.js 2.6 KB

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