Numeric constants and methods for Number objects

 

Number object



Definition: A Number object is a wrapper object for primitive values

When Number() is used as a constructor with operator new, it is a return value, which is used to return a newly created Number object. If you do not use new, but only call Number() as a function, it will Replace own argument with a primitive value and return that value (or NaN if conversion fails)

Writing method 1: var myNum1=new Number(value1);
Writing method 2: var myNum2=Number(value2);
Writing method 1: new represents that a new number object is created, and the newly created number object is returned, and value is the value of the new object.
Writing method 2: No new is written, which means that the value2 value is converted into a number-type value. If the conversion is unsuccessful, NaN is returned directly,
        This method can be used to convert other convertible types of data into Number type data.
The gap is very large, pay attention to one is to create, the other is to convert.
E.g:
(1)var myNum=Number("235");
document.write(myNum);//Return value 235
(2) var myNum1 = Number (5656);
document.write(myNum1);//Return value 5656
(3)var myNum2=Number("ass");
document.write(myNum2);//Return NaN
   Example 1 returns the value 235, Example 2 returns 5656, Example 3 returns NaN,

Numeric constants for Number objects:

   Coding can be simplified using the with operator
  如:with(Number) {
   . . . . . Numeric constant, function call.
             }
You can also use the notation "Number. Numeric constant". Note that it must be a dot, not a parenthesis. It should be noted that these numerical constants are the properties of the function Number() itself, not the properties of a separate Number object. ,
So when using it, you should use the Number. numeric constant directly, instead of creating a new object and calling the numeric constant.
      Commonly used numeric constants for Number include the following:
(1) MAX_VALUE represents the maximum value that can be used.
(2) MIN_VALUE represents the minimum value that can be used.
(3) POSITIVE_INFINITY represents positive infinity, and returns this value when it overflows.
(4) NEGATIVE_INFINITY represents negative infinity, and returns this value when it overflows.
(5) NaN is used to indicate that it is not a number.

Methods of Number Objects


Methods in Number:
(1) toString (base num)
      Definition: Convert a number to a string, (note to use the specified radix)
    That is, the form is: Number object.toString (the conversion base defaults to 10, which is decimal)
    The optional range of the radix is ​​2~36, the radix num is the base level, and the radix 2 means that the Number object will be converted into a binary string
(2) toLocaleString() 
      Definition: Converts numbers to strings and converts them using the native number format order. That is, you define a format for him to format the number.
    Create a new Number object a. Then call the method, a.toLocaleString(), with formatting requirements in parentheses. (If not filled, it will be displayed as it is)
   //number object method. toLocaleString() 
   var a = new Number(123456);
   document.write("<br>Original value display: "+a.toLocaleString());//123,456
   document.write("<br>Formatted display: "+a.toLocaleString("zh-Hans-CN-u-nu-hanidec")); //One two three, four five six
(3) toFixed (parameter num)
Definition: Converts a number to a string with the specified number of digits after the decimal point.
Create a new Number object b, the content of b is 123456.789. Then call this method b.toFixed(num); num specifies the number of digits after the decimal point, and num defaults to 0.
     1) If num is less than the number after the decimal point of the object itself, it will be rounded for display.
     2) If num is greater than the number after the decimal point of the object itself, then add 0 to the back, and add a few more 0s.
   //number object method. toFixed()
   var b=new Number(123456.789653);
    document.write("<br>No parameter value display (default is 0),:"+b.toFixed());//123457
    document.write("<br>There are two decimal places (the latter number will be rounded off):"+b.toFixed(2));//123456.79
    document.write("<br>The parameter is greater than the number of decimals of the object itself (zero-padded):"+b.toFixed(10));//123456.7896530000
(4) toExponential (parameter num)
Exponential is the adjective exponential, meaning power.
Definition: Used to convert the value of an object to exponential notation. The parameter num is optional and is used to specify the number of decimal places in exponential notation, that is, there is one digit before the decimal point and num digits after the decimal point.
   1) When the num parameter is null, the parameter will use more numbers by default, that is, the numerical value without particularly long decimal places will be displayed as a whole.
   2) When the parameter is less than the number of decimal points of the original object value, it is output by rounding, and the value of e+ is the number of digits that the decimal point moves forward.
   3) When the parameter is greater than the number of decimal points of the original object, the mantissa of the decimal point of the original object will be automatically filled with 0.
  //number object method: toExponential()
  var c=new Number(123.456000789);
document.write("<br>The parameter is empty by default:"+c.toExponential());//1.23456000789e+2
document.write("<br>参数2:"+c.toExponential(2));//1.23e+2
document.write("<br>参数为0:"+c.toExponential(0));//1e+2
document.write("<br>The parameter is 15:"+c.toExponential(15));//1.234560007890000e+2
(5) toPrecision (parameter num)
Precision is the precision of the noun, and the adjective is precise.
Definition: Used to format a number as a string of a specified length, in decimal. Note that the specified length is not the number of digits after the decimal point, but the total number of digits of all values.
  1) When the parameter num is null, it is equivalent to the method toString().
  2) When num is greater than the number of digits of the original object, zeros are automatically added behind.
  3) When num is less than the number of digits of the original object, it will be rounded for display.
//number object method: toPrecision()
var d = new Number (123456.03633);
document.write("<br>The specified precision parameter is empty:"+d.toPrecision());//123456.03633
       document.write("<br>The specified precision parameter is 2:"+d.toPrecision(2));//1.2e+5
document.write("<br>The specified precision parameter is 10:"+d.toPrecision(10));//123456.0363
document.write("<br>The specified precision parameter is 15:"+d.toPrecision(15));//123456.036330000


 (6) valueOf() 
    Definition: Returns the primitive numeric value of a Number object.
   There are no parameters in parentheses.
//Number object method: valueOf()
var e=new Number(36589.21356);
document.write("<br>Display the original value directly: "+e.valueOf());//36589.21356

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325783914&siteId=291194637