Permalink
Browse files

Fixes types/number number.round

  • Loading branch information...
arian committed Nov 9, 2012
1 parent 1d8ef5a commit 6dc8fafa8f5496e8c38c63d6ddca080d5fc7d60e
Showing with 23 additions and 5 deletions.
  1. +21 −4 test/types/number.js
  2. +2 −1 types/number.js
View
@@ -14,11 +14,18 @@ describe('types/number', function(){
})
describe('round', function(){
- it('should round numbers', function(){
+ it('should round numbers if no precision is specified', function(){
expect(number.round(3.14)).to.be(3)
- expect(number.round(3.74)).to.be(4)
- expect(number.round(3.74, 1)).to.be(3.7)
- expect(number.round(3.75, 1)).to.be(3.8)
+ })
+
+ it('should round numbers according to the units place specified', function(){
+ expect(number.round(0.01, 2)).to.equal(0.01)
+ expect(number.round(1, 3)).to.equal(1)
+ expect(number.round(-1.01)).to.equal(-1)
+ expect(number.round(-1.01, 2)).to.equal(-1.01)
+ expect(number.round(111, -1)).to.equal(110)
+ expect(number.round(-111, -2)).to.equal(-100)
+ expect(number.round(100, -5)).to.equal(0)
})
})
@@ -36,6 +43,13 @@ describe('types/number', function(){
expect(buffer3).to.eql([5, 5, 5, 5, 5])
expect(buffer4).to.eql([1, 1, 1, 1, 1])
})
+ it('should not call the function if the number is 0', function(){
+ var n = 0
+ number.times(0, function(){
+ n++
+ })
+ expect(n).to.be(0)
+ })
})
describe('random', function(){
@@ -46,6 +60,9 @@ describe('types/number', function(){
expect(random >= min && random <= max).to.be.ok()
}
})
+ it('should return the same number if the min and max are equal', function(){
+ expect(number.random(20, 20)).to.be(20)
+ })
})
})
View
@@ -16,7 +16,8 @@ var number = shell({
/*(number.round)?*/
round: function(precision){
- return parseFloat(number.toPrecision(this, precision))
+ precision = Math.pow(10, precision || 0).toFixed(precision < 0 ? -precision : 0)
+ return Math.round(this * precision) / precision
},/*:*/
/*(number.times)?*/

0 comments on commit 6dc8faf

Please sign in to comment.