
A collection of the Number Object methods and functions.
Every Math method is mirrored in the Number object, both as prototype and generic.
Returns the passed parameter as a Number, or null if not a number.
Number.from(arg);
Number.from('12') // returns 12 Number.from('hello') // returns null
Returns a random integer between the two passed in values.
var random = Number.random(min, max);
Number.random(5, 20); // returns a random number between 5 and 20.
Limits this number between two bounds.
myNumber.limit(min, max);
(12).limit(2, 6.5); // returns 6.5 (-4).limit(2, 6.5); // returns 2 (4.3).limit(2, 6.5); // returns 4.3
Returns this number rounded to the specified precision.
myNumber.round([precision]);
(12.45).round() // returns 12 (12.45).round(1) // returns 12.5 (12.45).round(-1) // returns 10
Executes the function passed in the specified number of times.
myNumber.times(fn[, bind]);
(4).times(alert); // alerts "0", then "1", then "2", then "3".
Returns this number as a float. Useful because toFloat must work on both Strings and Numbers.
myNumber.toFloat();
(111).toFloat(); // returns 111 (111.1).toFloat(); // returns 111.1
Returns this number as another number with the passed in base. Useful because toInt must work on both Strings and Numbers.
myNumber.toInt([base]);
(111).toInt(); // returns 111 (111.1).toInt(); // returns 111 (111).toInt(2); // returns 7
There are several methods available from the Math object that can be used as Number Methods.
(-1).abs(); // returns 1 (3).pow(4); // returns 81
© Linux.ria.ua, 2008-2024 |