preface

When reading a book saw the conversion of the base system, I could not help but let myself contact with my poor grades of students slag, conversion of a base system but also count along while, alas, from childhood to mathematics on my ruthless ah. And I always forget, as long as I don’t often forget, so here is a brief note

The data type

ECMAScript has six simple data types (also known as primitive types) : Undefined, Null, Number, Boolean, String, and Symbol.

Symbol is an addition to ECMAScript6.

There is another complex data type, Object.

In ECMAScript, all values can be represented by one of the seven data types mentioned above.

In this article, we will record several base conversions of Number type.

Different numeric types have different numeric literal formats

Octal, hexadecimal, binary conversion to decimal

1. The decimal

The most basic numeric literal format is a decimal integer, which can be written directly:

let num = 520; / / integerCopy the code

Decimal integers can also be expressed as octal (base 8) or hexadecimal (base 16) literals.

2. The octal

For octal literals, they must be prefixed with 0, followed by the corresponding octal digit, which ranges from 0 to 7. If the literal contains more digits than it should, the prefixed 0 is ignored and the following sequence of digits is treated as a decimal number, as shown below:

let num1 = 050; // octal 40 let num2 = 080; // Invalid octal value, treat as 80 let num3 = 09; // Invalid octal value, treated as 9Copy the code

Octal to decimal: (units digit multiplied by 8 to the 0) + (tens digit multiplied by 8 to the 1) + (hundreds digit multiplied by 8 to the 2)… = decimal number

For example, num1 = 050 is 0×8º + 5×8¹ + 0×8² = 40. For example, num4 = 052 is 2×8º + 5×8¹ + 0×8² = 42

  • Anything raised to the 0 power, except 0, is equal to 1
  • Octal values in ECMAScript 2015 or ES6 are represented by the prefix 0O; In strict mode, the prefix 0 is treated as a syntax error if it is represented

Octal values should be prefixed with 0o

3. Hexadecimal

Hexadecimal literals prefixed with 0x (case sensitive) and hexadecimal digits 0, 9, and A, F. (the binary digits represented by ABCDEF are 10,11,12,13,14,15). The hexadecimal digits are both uppercase and lowercase, as shown below:

let num1 = 0xA; // hex 10 let num2 = 0x1f; // Hexadecimal 31Copy the code

Conversion from hexadecimal to decimal:

(the units digit times 16 to the 0) + (the tens digit times 16 to the 1) + (the hundreds digit times 16 to the 2)... = decimal number

For example, num2 = 0x1f in the above code is 15×16º + 1×16¹ = 31

4. The binary

Binary literals, with only 0 and 1 values, are represented in ES6, starting with 0b, as follows:

let num1 = 0b101; // let num2 = 0b10011; // Binary 19Copy the code

Binary to decimal calculation method:

(units digit times 2 to the 0) + (tens digit times 2 to the 1) + (hundreds digit times 2 to the 2)... = decimal number

For example, num1 = 0b101, which is 1×2º + 0×2¹ + 1×2² = 5, num2 = 0b10011, you can calculate the decimal value

Decimal to binary, octal, hexadecimal

1. Switch from decimal to binary

Using the division of 2 mod, reverse order arrangement method

Divide the decimal number by 2 to get the quotient and remainder. Divide the quotient by 2 to get another quotient and remainder. Repeat the process until the quotient equals 0

For example: what is 89 converted to binary?
44 members present more than 89 members present 2 = 44 | 1 2 = more than 22 | 0 more than 22 members present 2 = 11 | 0 11 present 2 = 5 more than more than | | 1 5 present 2 = 2 1 2 present more than 2 = 1 | 0 1 present 2 = 0 | 1Copy the code

All the remainder is: 1001 101 Reverse all the remainder, the result is: 1011001, so 89 converted to binary, is equal to 1011001

2. Decimal to octal

Adopt divide 8 to take mod, reverse order arrangement method

The calculation process is similar to that of converting from decimal to binary

For example: what is 21 converted to octal?
More than 21 members present 8 = 2 | 5 more than 2 members present 8 = 0 | 2Copy the code

All the remainder is: 5, 2 Reverse all the remainder, the result is: 25, octal starts with 0, so 21 converted to octal equals 025

3. Convert from decimal to hexadecimal

Adopt division 16 mod, reverse order arrangement method

The calculation process is similar to that of converting from decimal to binary

For example: what is 67 in hexadecimal?
More than 67 members present 16 = 4 | 3 more than 4 members present 16 = 0 | 4Copy the code

All the remainder is: 3, 4 reverse all the remainder, the result is: 43, the hexadecimal prefix is 0x, so 67 converted to hexadecimal, the result is equal to 0x43

Base conversions don’t seem to be used much on the front end, but it’s good to have a quick overview

Ok, so much for the conversion record on base, here is the extended record on floating point values

Extended record:

Talk about floating point values: Floating point values, which must contain a decimal point and must be followed by at least one number. It is not necessary to precede the decimal point with an integer, but it is recommended to do so, as shown below:

Let floatNum1 = 5.2; Let floatNum1 = 0.2; let floatNum1 = .2; // Valid, but not recommendedCopy the code

Because storing floating-point values uses twice as much memory as storing integer values, ECMAScript always tries to convert values to integers. When there are no numbers after the decimal point, the value becomes an integer. Similarly, if the value itself is an integer followed by a 0 (such as 6.0), it will be converted to an integer as follows:

let floatNum1 = 5.; // Let floatNum1 = 6.0; // After the decimal point is 0, treat as integer 6Copy the code

Floating-point values can be expressed in scientific notation for extremely large or extremely small values. Scientific notation is the representation of a number multiplied by 10 to the NTH power in the form of a number (integer or floating point) followed by an uppercase or lowercase letter e, plus a number of powers of 10 to be multiplied. Such as:

Let floatNum = 5.21 e7; / / equal to 52100000Copy the code

Let floatNum = 5.21e7 (10 to the seventh

Very small values are encountered, such as 0.000000002, which can be represented by scientific notation as 2E-9

By default, ECMAScript converts floating point values containing at least six zeros after the decimal point to scientific notation

For example, 0.000 000 3 would be converted to 3E-7

Accuracy of floating-point values

Floating-point values are accurate to up to 17 decimal places, but are far less accurate in arithmetic than integers. For example, instead of adding 0.2 to 0.1, you get 0.300 000 000 000 000 04.

Such minor errors make it difficult to test specific floating point values, such as:

If (a + b == 0.3) { Console. log(" Result: 0.3."); }Copy the code

This tests whether the sum of the two values equals 0.3. If the two values are 0.05 and 0.25, or 0.15 and 0.15, that’s fine. But if it is 0.1 and 0.2, as mentioned above, the test will fail. So never test a particular floating point value

That’s the end of this article, thanks for your patience ❤