Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixes types/number number.round

  • Loading branch information...
commit 6dc8fafa8f5496e8c38c63d6ddca080d5fc7d60e 1 parent 1d8ef5a
Arian Stolwijk authored November 10, 2012
25  test/types/number.js
@@ -14,11 +14,18 @@ describe('types/number', function(){
14 14
     })
15 15
 
16 16
     describe('round', function(){
17  
-        it('should round numbers', function(){
  17
+        it('should round numbers if no precision is specified', function(){
18 18
             expect(number.round(3.14)).to.be(3)
19  
-            expect(number.round(3.74)).to.be(4)
20  
-            expect(number.round(3.74, 1)).to.be(3.7)
21  
-            expect(number.round(3.75, 1)).to.be(3.8)
  19
+        })
  20
+
  21
+        it('should round numbers according to the units place specified', function(){
  22
+            expect(number.round(0.01, 2)).to.equal(0.01)
  23
+            expect(number.round(1, 3)).to.equal(1)
  24
+            expect(number.round(-1.01)).to.equal(-1)
  25
+            expect(number.round(-1.01, 2)).to.equal(-1.01)
  26
+            expect(number.round(111, -1)).to.equal(110)
  27
+            expect(number.round(-111, -2)).to.equal(-100)
  28
+            expect(number.round(100, -5)).to.equal(0)
22 29
         })
23 30
     })
24 31
 
@@ -36,6 +43,13 @@ describe('types/number', function(){
36 43
             expect(buffer3).to.eql([5, 5, 5, 5, 5])
37 44
             expect(buffer4).to.eql([1, 1, 1, 1, 1])
38 45
         })
  46
+        it('should not call the function if the number is 0', function(){
  47
+            var n = 0
  48
+            number.times(0, function(){
  49
+                n++
  50
+            })
  51
+            expect(n).to.be(0)
  52
+        })
39 53
     })
40 54
 
41 55
     describe('random', function(){
@@ -46,6 +60,9 @@ describe('types/number', function(){
46 60
                 expect(random >= min && random <= max).to.be.ok()
47 61
             }
48 62
         })
  63
+        it('should return the same number if the min and max are equal', function(){
  64
+            expect(number.random(20, 20)).to.be(20)
  65
+        })
49 66
     })
50 67
 
51 68
 })
3  types/number.js
@@ -16,7 +16,8 @@ var number = shell({
16 16
 
17 17
     /*(number.round)?*/
18 18
     round: function(precision){
19  
-        return parseFloat(number.toPrecision(this, precision))
  19
+        precision = Math.pow(10, precision || 0).toFixed(precision < 0 ? -precision : 0)
  20
+        return Math.round(this * precision) / precision
20 21
     },/*:*/
21 22
 
22 23
     /*(number.times)?*/

0 notes on commit 6dc8faf

Please sign in to comment.
Something went wrong with that request. Please try again.