JavaScript does not distinguish between integer values and floating point values. All numbers in JavaScript are stored in the form of 64-bit floating point numbers, including integers. For example, 2 and 2.0 are the same number; so pay special attention to the problem of missing progress when performing number operations.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Number type represents number, JavaScript adopts "double-precision 64-bit format defined by IEEE 754 standard" IEEE 754 values") represent numbers.
Different from other programming languages (such as C and Java), JavaScript does not distinguish between integer values and floating point values. All numbers are represented by floating point values in JavaScript, so when performing numbers Pay special attention to the problem of missing progress when calculating.
0.1 + 0.2 = 0.30000000000000004; 0.1 + 0.2 == 0.3; // false // 浮点运算判断相等 var ACCURACY = 1e-5; //定义精度精确到0.00001 var a = 0.1; var b = 0.2; var sum = 0.3; // 判断相差小于精度就认为相等 if (a + b - sum < ACCURACY) { console.log('a+b == sum'); }
In specific implementations, integer values are usually treated as 32-bit integer variables. In individual implementations (such as some browsers), they are also stored in the form of 32-bit integer variables until it is Used to perform certain operations that are not supported by 32-bit integers. This is to facilitate bit operations.
You can omit 0 to represent decimals, or you can use exponential form to represent numbers
.9; // 0.9 1E3 // 1000 2e6 // 2000000 0.1e2 // 10 1e-5 // 0.00001
Number can use four numerical systems: decimal, binary, octal and hexadecimal. Only use integers for non-decimal numbers.
// 十进制 12345678 42 0777 // 在非严格格式下会被当做八进制处理 (用十进制表示就是511) // 二进制 var a = 0b100000; // 32 var b = 0b0111111; // 63 var c = 0B0000111; // 7 // 八进制 var n = 0755; // 493 var m = 0644; // 420 var a = 0o10; // ES6 :八进制 // 十六进制 0xFFFFFFFFFFFFFFFFF // 295147905179352830000 0x123456789ABCDEF // 81985529216486900 0XA // 10
Hexadecimal conversion mainly uses Number's toString() method or parseInt() method:
// toString转换,输入为Number,返回为String var n = 120; n.toString(); // "120" n.toString(2); // "1111000" n.toString(8); // "170" n.toString(16); // "78" n.toString(20); // "60" 0x11.toString(); // "17" 0b111.toString(); // "7" 0x11.toString(12);// "15" // parseInt转换,输入为String,返回为Number parseInt('110'); // 110 parseInt('110', 2); // 6 parseInt('110', 8); // 72 parseInt('110', 16); // 272 parseInt('110', 26); // 702 // toString和parseInt结合使用可以在两两进制之间转换 // 将 a 从36进制转为12进制 var a = 'ra'; // 36进制表示的数 parseInt(a, 36).toString(12); // "960"
The Number object is a built-in number object that encapsulates a series of Number-related constants and methods.
var na = Number(8); // 8 na = Number('9.5'); // 9.5 na = new Number('9.3'); // Number 对象,仅可以使用原型方法
Properties | Description |
---|---|
Number.MAX_VALUE | The maximum representable value |
##Number.MIN_VALUE | The smallest representable value|
Number.NaN
| Specially refers to "non-number"|
Number.NEGATIVE_INFINITY
| Specifically refers to "negative infinity"; returns upon overflow|
Number.POSITIVE_INFINITY
| refers specifically to "positive infinity"; upon overflow, it returns |
Number.EPSILON
| represents 1 and The difference between the smallest Number that is closest to 1 and greater than 1|
Number.MIN_SAFE_INTEGER
| JavaScript Minimum Safe Integer|
Number.MAX_SAFE_INTEGER
| JavaScript Maximum Safe Integer
方法 | 描述 |
---|---|
Number.parseFloat() | 把字符串参数解析成浮点数,左右等效于一元运算法+ |
Number.parseInt() | 把字符串解析成特定基数对应的整型数字 |
Number.isFinite() | 判断传递的值是否为有限数字 |
Number.isInteger() | 判断传递的值是否为整数 |
Number.isNaN() | 判断传递的值是否为 NaN |
Number.isSafeInteger() | 判断传递的值是否为安全整数 |
parseInt() 方法需要注意:
parseInt('123'); // 123 parseInt('123', 10); // 123 parseInt('123', 8); // 83 parseInt('123', 16); // 291 parseInt("11", 2); // 3 parseInt('0x123', 10); // 0 parseInt('0x123', 16); // 291 parseInt('0x123'); // 291 // 如果第一个参数不是字符串,会先把第一个参数转成字符串 parseInt('12', 16); // 18 parseInt(12, 16); // 18 // toString方法会将数字转换为10进制的字符串 parseInt(0xf, 16); // 21 0xf.toString(); // '15' parseInt('15', 16); // 21 parseInt('23.56'); // 23 parseInt("hello", 16); // NaN parseInt("aello", 16); // 174
Number类型原型上还有一些方法来对数字进度进行取舍,这些方法可以被 Number 实例对象调用:
方法 | 描述 |
---|---|
toExponential() | 返回一个数字的指数形式的字符串 |
toFixed() | 返回指定小数位数的表示形式 |
toPrecision() | 返回一个指定精度的数字 |
这些原型方法可以被Number实例对象调用:
var numObj = 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(); // 返回 "12346":进行四舍五入,不包括小数部分 numObj.toFixed(1); // 返回 "12345.7":进行四舍五入 numObj.toFixed(6); // 返回 "12345.678900":用0填充 (1.23e+20).toFixed(2); // 返回 "123000000000000000000.00" (1.23e-10).toFixed(2); // 返回 "0.00" 2.34.toFixed(1); // 返回 "2.3" -2.34.toFixed(1); // 返回 -2.3 (由于操作符优先级,负数不会返回字符串) (-2.34).toFixed(1); // 返回 "-2.3" (若用括号提高优先级,则返回字符串)
和Number相关的是,JavaScript对象中内置的Math对象,提供了很多数学常数和函数的属性和方法。
属性列表:
属性 | 描述 |
---|---|
Math.E |
欧拉常数,也是自然对数的底数, 约等于 2.718 |
Math.LN2 |
2的自然对数, 约等于0.693 |
Math.LN10 |
10的自然对数, 约等于 2.303 |
Math.LOG2E |
以2为底E的对数, 约等于 1.443 |
Math.LOG10E |
以10为底E的对数, 约等于 0.434 |
Math.PI |
圆周率,一个圆的周长和直径之比,约等于 3.14159 |
Math.SQRT2 |
2的平方根,约等于 1.414 |
Math.SQRT1_2 |
1/2的平方根, 约等于 0.707 |
The trigonometric function sin and other parameters in Math are radians. If it is an angle, you can use it (Math.PI / 180)
Method | Description |
---|---|
##Math.abs(x)
| Returns the absolute value of x|
Math.sign(x)
| Returns the sign function of x to determine whether x is a positive number, a negative number or 0|
Math .random()
| Returns a pseudo-random number between 0 and 1|
Return the value of x after rounding up |
|
Return the value of x after rounding up |
|
Returns the rounded integer. |
|
Returns the nearest single-precision floating point representation of a number |
|
Return the integer part of x, removing the decimal number |
|
Return the square root of x |
|
Returns the cube root of x |
|
Returns the square root of the sum of the squares of its parameters |
|
Returns the y power of x |
min(), max() |
sin(), cos(), tan() | |
asin(), acos(), atan(), atan2() | |
sinh(), cosh(), tanh() | |
##asinh(), acosh(), atanh() | |
pow(), exp(), expm1(), log10(), log1p( ), log2() | |
[Related recommendations: |
The above is the detailed content of JavaScript separates integers and floating point numbers. For more information, please follow other related articles on the PHP Chinese website!