Skip to content

Commit

Permalink
More number exts.
Browse files Browse the repository at this point in the history
  • Loading branch information
vesln committed Jan 31, 2012
1 parent d607ff1 commit f332532
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
52 changes: 51 additions & 1 deletion lib/number.js
Expand Up @@ -8,4 +8,54 @@
/**
* Module dependencies.
*/
var mill = require('mill');
var mill = require('mill');

/**
* Converts number to integer.
*
* @returns {Number}
* @api public
*/
Number.prototype.toInt = function() {
return parseInt(this);
};

/**
* Returns the absolute value of a specified number.
*
* @returns {Number}
* @api public
*/
Number.prototype.abs = function() {
return Math.abs(this);
};

/**
* Rounds a number to the nearest integer.
*
* @returns {Number}
* @api public
*/
Number.prototype.round = function() {
return Math.round(this);
};

/**
* Rounds a number UPWARDS to the nearest integer.
*
* @returns {Number}
* @api public
*/
Number.prototype.ceil = function() {
return Math.ceil(this);
};

/**
* Rounds a number DOWNWARDS to the nearest integer.
*
* @returns {Number}
* @api public
*/
Number.prototype.floor = function() {
return Math.floor(this);
};
30 changes: 30 additions & 0 deletions test/number.test.js
Expand Up @@ -18,4 +18,34 @@ describe('Number', function() {
(1)[method].should.be.ok;
});
});

describe('.toInt()', function() {
it('should convert number to integer', function() {
(5.5).toInt().should.eql(5);
});
});

describe('.abs()', function() {
it('should return the abs value of a number', function() {
(-1).abs().should.eql(1);
});
});

describe('.round()', function() {
it('should rounds a number to the nearest integer', function() {
(2.6).round().should.eql(3);
});
});

describe('.ceil()', function() {
it('should rounds a number to the nearest integer', function() {
(2.2).ceil().should.eql(3);
});
});

describe('.floor()', function() {
it('should rounds a number to the nearest integer', function() {
(2.2).floor().should.eql(2);
});
});
});

0 comments on commit f332532

Please sign in to comment.