The Number method is relatively small. There are eight methods in total:
ToColorPart: converts a Number object to a hexadecimal format with two digits
Succ: returns the next value of the current Number object, that is, the current value plus one.
Times: uses the Ruby style to encapsulate a standard [0... n] loop.
ToPaddedString: converts the current Number object to a string. If the length of the converted string is smaller than the value specified by length, use 0 to fill in the remaining digits on the left.
Abs: returns the absolute value of the current Number object.
Round: returns the integer after rounding the current Number object.
Ceil: returns the smallest integer greater than or equal to the current Number object.
Floor: returns the maximum integer less than or equal to the current Number object.
One of the important methods is toPaddedString. The Number object overrides the toString method:
NumberObject. toString (radix)
Parameters |
Description |
Radix |
Optional. Indicates the base number of a number, so that 2 ~ An integer between 36. If this parameter is omitted, base 10 is used. Note that if this parameter is a value other than 10, the ECMAScript standard allows any value to be returned. |
Copy codeThe Code is as follows:
Function toPaddedString (length, radix ){
Var string = this. toString (radix | 10); // convert the number to the corresponding hexadecimal notation.
Return '0'. times (length-string. length) + string; // times method extended in String, repeat a CHARACTER n times
}
With this method, a useful extension is toColorPart, which can be used for color conversion in CSS:
Copy codeThe Code is as follows:
Function toColorPart (){
Return this. toPaddedString (2, 16 );
}
Since it is CSS color conversion, numbers must be in the range of [0-255.
Console. log (10). toColorPart (); // 0a
There is a succ method with the same name as the String method. In String, it is incrementing according to the orders table, and in Number, it is in the order of natural numbers.
Copy codeThe Code is as follows:
Function succ (){
Return this + 1;
}
Console. log (10). succ (); // 11
Starting from this method, a simple array of 0-n
Copy codeThe Code is as follows:
Function range (){
Var ret = [0];
For (var I = 0; I <this-1; I ++ ){
Ret. push (I. succ ());
}
Return ret;
}
Console. log (10). range (); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Use this range Function to obtain the times function temporarily:
Copy codeThe Code is as follows:
Function times (iterator, context ){
This. range (). forEach (iterator, context); // The R () method is used in the source code.
Return this;
}
Copy codeThe Code is as follows:
Var s = '';
(5). times (function (item ){
S + = item;
});
Console. log (s); // 01234
Apart from the above methods, other methods are to extend the static Math method to the Number object. =]
Copy codeThe Code is as follows:
Function abs (){
Return Math. abs (this );
}
Function round (){
Return Math. round (this );
}
Function ceil (){
Return Math. ceil (this );
}
Function floor (){
Return Math. floor (this );
}