Numbers

Number.toInt, .toFloat

Number.toInt() just returns the number; useful because toInt must work on both Strings and Numbers. Note that if you call .toInt() on a float, you'll get an integer back.

var x = 10;
x.toInt(); //returns 10
execute this code
var x = 10.01;
x.toFloat(); //returns 10.01
execute this code
var x = 10.01;
x.toInt(); //returns 10
execute this code

Number.limit

(12).limit(2, 6.5)  // returns 6.5
execute this code
(-4).limit(2, 6.5)  // returns 2
execute this code
(4.3).limit(2, 6.5) // returns 4.3
execute this code

Number.round

Returns the number rounded to specified precision: the number of digits after the decimal point. Can also be negative or zero (default).

12.45.round() // returns 12
execute this code
12.45.round(1) // returns 12.5
execute this code
12.45.round(-1) // returns 10
execute this code

Number.times

Executes a passed in function the specified number of times:

(3).times(alert)
//alert(0)
//alert(1)
//alert(2)
execute this code

mootorial/03-native/02-number.txt · Last modified: 2011/01/14 03:48 by ralph