Number question
Are you able to answer the questions below?
- 0.1 + 0.2 = = 0.3 established?
- What does the. e-5 mean?
- How to express 8 binary?
- How do I convert into a system?
- How do I convert a string to a numeric or integer? What's the reverse? What does hex do with it?
- What is the return value of parseint (0x12, 16)? Is it 0x12?
- Number.MAX_VALUE is the maximum value, (new number (12)). How much is Max_value?
- How do I round up in JavaScript? What if I keep the precision of 3 decimal places?
- How do I get a random number? How to take the whole? How do I take the rounding up?
Number Representation method
The number type represents numbers, and JavaScript uses the "double-precision 64-bit format defined by the IEEE 754 standard" ("Double-precision 64-bit format IEEE 754 values") to represent numbers.
Unlike other programming languages, such as C and Java, JavaScript does not differentiate between integer values and floating-point values, and all numbers are represented by floating-point numbers in JavaScript, so pay particular attention to the lack of progress when doing digital operations.
0.1 + 0.2 = 0.30000000000000004;0.1 + 0.2 == 0.3; //False///floating-point operations are judged equalvarAccuracy= 1e-5; //definition accuracy to 0.00001varA= 0.1;varB= 0.2;varSum= 0.3;//Judgment difference less than precision is considered equalif(A+B-Sum<Accuracy){ Console.Log(' a+b = = Sum ');}
In a concrete implementation, an integer value is usually treated as a 32-bit integer variable, and is stored as a 32-bit integer variable in an individual implementation (such as some browsers) until it is used to perform operations that are not supported by some 32-bit integers, for ease of bit operation.
You can use omit 0 to denote decimals, or you can represent numbers in exponential form
.9; // 0.91E3 // 10002e6 // 20000000.1e2// 101e-5 // 0.00001
The representation of number numbers in different binary systems
Number can be used in four decimal digits, binary, octal, and hexadecimal. Non-decimal is used only with integers.
- Binary notation: Starting with zero, followed by a lowercase or uppercase Latin letter B (0b or 0B)
- Octal notation: Starts with 0. If the number after 0 is not within the range of 0 to 7, the number will be converted to a decimal number.
- Using octal syntax in ECMAScript 5 strict mode is considered a decimal
- Octal numbers in ECMAScript 6 are required to prefix a number with "0o"
- Hexadecimal notation: Starting with zero, followed by a lowercase or uppercase Latin letter X (0x or 0X)
//Decimal12345678 the0777 //In non-strict format will be treated as octal (in decimal notation is 511)//BinaryvarA=0b100000; //varB=0b0111111; //varC=0b0000111; //7//Eight binaryvarN= 0755; //493varM= 0644; //420varA= 0o10; //ES6: Octal//16 binary0xFFFFFFFFFFFFFFFFF //2951479051793528300000x123456789abcdef //819855292164869000XA //Ten
Conversion of a different binary
The conversion of the binary mainly uses the ToString () method of number or the parseint () method:
- The ToString () method accepts a value of 2~36 as an integer parameter that specifies the binary, which defaults to decimal, and number to string
- parseint () The second parameter accepts a value of 2~36 as an integer parameter that specifies the binary, which defaults to decimal and the string to number
//ToString conversion, input as number, returned as StringvarN= -;N.toString(); //"+"N.toString(2); //"1111000"N.toString(8); //"the"N.toString( -); //"+"N.toString( -); //" "0x11.toString(); //"+"0b111.toString(); //"7"0x11.toString( A);//" the"//parseint conversion, input as String, return as numberparseint(' a '); //parseint(' a ', 2); //6parseint(' a ', 8); //parseint(' a ', -); //272parseint(' a ', -); //702//ToString and parseint can be converted between 22 binary//Convert a from 36 into 12 binaryvarA= ' RA '; //36 number of binary representationsparseint(A, $).toString( A); //"960"
Numeric Object number
The number object is a built-in numeric object that encapsulates a series of numbers related constants and methods.
var=Number(8); // 8=Number(‘9.5‘); // 9.5=newNumber(‘9.3‘);// Number 对象,仅可以使用原型方法
The property of the Number object:
Properties |
Description |
number.max_value |
|
number.min_value |
|
Number.NaN |
|
number.negative_infinity |
|
number.positive_infinity |
|
Number.epsilon |
|
number.min_safe_integer |
JavaScript minimum security integer |
number.max_safe_integer |
javascript maximum security integer |
Number Object method
The Number object method can use number. Call, or you can use global invocation.
Method |
Description |
Number.parseFloat() |
Parse the string parameter into a floating-point number, which is equivalent to the unary operation Method + |
Number.parseInt() |
Parses a string into an integer number corresponding to a specific cardinality |
Number.isFinite() |
Determines whether the passed value is a finite number |
Number.isInteger() |
Determines whether the passed value is an integer |
Number.isNaN() |
Determines whether the passed value is NaN |
Number.isSafeInteger() |
Determines whether the passed value is a safe integer |
The parseint () method requires attention:
- The parseint () parameter can have two parameters: a numeric string and a binary
- The result is 0 if the first argument is a numeric string that is not a specified binary
- If the first argument is not a string, the ToString () method that first calls the parameter is converted to a string
- Characters that are not specified in the first parameter are ignored
- If the given string does not have a numeric form, the function returns a special value NaN
- If the second argument is not supplied when the call is made, the number of digits represented by the string is used as a binary
parseint(' 123 '); //123parseint(' 123 ', Ten); //123parseint(' 123 ', 8); ///parseint(' 123 ', -); //291parseint("One", 2); //3parseint(' 0x123 ', Ten); //0parseint(' 0x123 ', -); //291parseint(' 0x123 '); //291//If the first argument is not a string, the first argument is first converted to a stringparseint(' a ', -); //parseint( A, -); //The //ToString Method converts a number to a 10-binary stringparseint(0xf, -); //0xf.toString(); //' "parseint(' a ', -); //parseint(' 23.56 '); //parseint("Hello", -); //NaNparseint("Aello", -); //174
Number Type prototype method
The number type prototype also has methods to trade-offs of digital progress, which can be called by the numbers instance object:
Method |
Description |
toExponential() |
Returns a string in exponential form of a number |
toFixed() |
Returns the representation of the specified number of decimal digits |
toPrecision() |
Returns a number of the specified precision |
These prototype methods can be called by the number instance object:
varNumobj= 12345.6789;Numobj.toexponential(); //"1.23456789e+4"Numobj.toexponential(2); //"1.23e+4"Numobj.toexponential(4); //"1.2346e+4"Numobj.toprecision(); //"12345.6789"Numobj.toprecision(2); //"1.2e+4"Numobj.toprecision(4); //"1.235e+4"Numobj.toFixed(); //Return "12346": rounding, excluding decimal partsNumobj.toFixed(1); //Return to "12345.7": RoundingNumobj.toFixed(6); //Return "12345.678900": Fill with 0(1.23e+20).toFixed(2); //Return to "123000000000000000000.00"(1.23e-10).toFixed(2); //Return to "0.00"2.34.toFixed(1); //Return to "2.3"-2.34.toFixed(1); //Return-2.3 (negative numbers will not return a string due to operator precedence)(-2.34).toFixed(1); //returns "2.3" (returns a string if the precedence is increased with parentheses)
Math Object (Math)
Related to number is the math object built into the JavaScript object, providing many of the mathematical constants and functions of the properties and methods.
Property list:
Properties |
Description |
math.e |
/tr> |
math.ln2 |
2 natural logarithm, approximately equals 0.693 |
math.ln10 |
10 natural logarithm, approximately equal to 2.303 |
math.log2e |
|
math.log10e |
|
math.pi |
Pi, the circumference and diameter of a circle, approximately equals 3.14159 |
math.sqrt2 |
2 square root, approximately equals 1.414 |
math.sqrt1_2 |
1/2, approximately equals 0.707 |
The trigonometric function in Math, such as sin, is radians, which can be used if the angle is (math.pi/180)
Method |
Description |
Math.abs(x) |
Returns the absolute value of X |
Math.sign(x) |
Returns the symbol function of x, which determines whether x is positive, negative or 0 |
Math.random() |
Returns the pseudo-random number from 0 to 1 |
Math.floor(x) |
Returns the value of X rounding up |
Math.ceil(x) |
Returns the value of X rounding up |
Math.round(x) |
Returns the rounded integer. |
Math.fround(x) |
Returns the nearest single-precision floating-point representation of a number |
Math.trunc(x) |
Returns the integer portion of x, removing decimals |
Math.sqrt(x) |
Returns the square root of X |
Math.cbrt(x) |
Returns the cube root of x |
Math.hypot([x[,y[,…]]]) |
Returns the square root of its parameter squared sum |
Math.pow(x,y) |
Returns the Y power of X |
Min (), Max () |
Returns a small or large value in a comma-separated list of numeric parameters (respectively) |
Sin (), cos (), tan () |
Standard trigonometric functions; radians |
ASIN (), ACOs (), Atan (), atan2 () |
Inverse trigonometric function The return value is radians |
Sinh (), cosh (), Tanh () |
Double-curvature trigonometric functions; The return value is radians. |
Asinh (), Acosh (), Atanh () |
inverse hyperbolic trigonometric functions; The return value is radians. |
POW (), exp (), EXPM1 (), log10 (), log1p (), log2 () |
Exponential and logarithmic functions |
Original: http://uusama.com/571.html
Time: 5 hours
JavaScript Numeric Number Type detailed