You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/05-data-types/02-number/article.md
+13-13Lines changed: 13 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ Imagine we need to write 1 billion. The obvious way is:
12
12
let billion =1000000000;
13
13
```
14
14
15
-
But in real life we usually avoid writing the full number as it's easy to mistype. Also, we are lazy. We will usually write something like `"1bn"` for a billion or `"7.3bn"` for 7 billion 300 million. The same is true for most large numbers.
15
+
But in real life we usually avoid writing a long string of zeroes as it's easy to mistype. Also, we are lazy. We will usually write something like `"1bn"` for a billion or `"7.3bn"` for 7 billion 300 million. The same is true for most large numbers.
16
16
17
17
In JavaScript, we shorten a number by appending the letter `"e"` to the number and specifying the zeroes count:
18
18
@@ -125,7 +125,7 @@ There are several built-in functions for rounding:
125
125
: Rounds to the nearest integer:`3.1` becomes `3`, `3.6` becomes `4` and `-1.1` becomes `-1`.
126
126
127
127
`Math.trunc` (not supported by Internet Explorer)
128
-
: Removes anything after the decimal without rounding:`3.1` becomes `3`, `-1.1` becomes `-1`.
128
+
: Removes anything after the decimal point without rounding:`3.1` becomes `3`, `-1.1` becomes `-1`.
129
129
130
130
Here's the table to summarize the differences between them:
131
131
@@ -145,7 +145,7 @@ There are two ways to do so:
145
145
146
146
1. Multiply-and-divide.
147
147
148
-
For example, to round the number to the 2nd digit after the decimal, we can multiply the number by `100`, call the rounding function, and then divide back.
148
+
For example, to round the number to the 2nd digit after the decimal, we can multiply the number by `100`, call the rounding function and then divide it back.
149
149
```js run
150
150
let num = 1.23456;
151
151
@@ -159,7 +159,7 @@ There are two ways to do so:
159
159
alert( num.toFixed(1) ); // "12.3"
160
160
```
161
161
162
-
This round up or down to the nearest value, similar to `Math.round`:
162
+
This rounds up or down to the nearest value, similar to `Math.round`:
163
163
164
164
```js run
165
165
let num = 12.36;
@@ -205,7 +205,7 @@ Ouch! There are more consequences than an incorrect comparison here. Imagine you
205
205
206
206
But why does this happen?
207
207
208
-
A number is stored in memory in it's binary form, a sequence of ones and zeroes. But fractions like `0.1`, `0.2` that look simple in the decimal numeric system are actually unending fractions in their binary form.
208
+
A number is stored in memory in its binary form, a sequence of ones and zeroes. But fractions like `0.1`, `0.2` that look simple in the decimal numeric system are actually unending fractions in their binary form.
209
209
210
210
In other words, what is `0.1`? It is one divided by ten `1/10`, one-tenth. In decimal numeral system such numbers are easily representable. Compare it to one-third: `1/3`. It becomes an endless fraction `0.33333(3)`.
211
211
@@ -254,7 +254,7 @@ Can we work around the problem? Sure, there're a number of ways:
254
254
255
255
This works because when we do `0.1*10 = 1` and `0.2 * 10 = 2` then both numbers become integers, and there's no precision loss.
256
256
257
-
3. If it's we were dealing with a shop, then the most radical solution would be to store all prices in cents and use no fractions at all. But what if we apply a discount of 30%? In practice, totally evading fractions is rarely feasible, so the solutions above help avoid this pitfall.
257
+
3. If we were dealing with a shop, then the most radical solution would be to store all prices in cents and use no fractions at all. But what if we apply a discount of 30%? In practice, totally evading fractions is rarely feasible, so the solutions above help avoid this pitfall.
258
258
259
259
````smart header="The funny thing"
260
260
Try running this:
@@ -320,7 +320,7 @@ let num = +prompt("Enter a number", '');
320
320
alert( isFinite(num) );
321
321
```
322
322
323
-
Please note that spaces in a string are treated as a `0` in all numeric functions, including `isFinite`.
323
+
Please note that an empty or a space-only string is treated as `0` in all numeric functions including `isFinite`.
324
324
325
325
```smart header="Compare with `Object.is`"
326
326
@@ -331,7 +331,7 @@ There is a special built-in method [Object.is](mdn:js/Object/is) that compares v
331
331
332
332
In all other cases, `Object.is(a, b)` is the same as `a === b`.
333
333
334
-
This method of comparison is often used in JavaScript. When an internal algorithm needs to compare two values for being exactly the same, it uses `Object.is` (internally called [SameValue](https://tc39.github.io/ecma262/#sec-samevalue)).
334
+
This way of comparison is often used in JavaScript specification. When an internal algorithm needs to compare two values for being exactly the same, it uses `Object.is` (internally called [SameValue](https://tc39.github.io/ecma262/#sec-samevalue)).
335
335
```
336
336
337
337
@@ -343,19 +343,19 @@ Numeric conversion using a plus `+` or `Number()` is strict. If a value is not e
343
343
alert( +"100px" ); // NaN
344
344
```
345
345
346
-
The sole exception is spaces before and after the line, as they are ignored.
346
+
The sole exception is spaces at the beginning or at the end of the string, as they are ignored.
347
347
348
-
But in real life we often have values in units, like `"100px"` or `"12pt"` in CSS. Also in many countries, the currency symbol goes after the amount, so we have `"19€"`, but would still like to extract a numeric value out of that.
348
+
But in real life we often have values in units, like `"100px"` or `"12pt"` in CSS. Also in many countries the currency symbol goes after the amount, so we have `"19€"` and would like to extract a numeric value out of that.
349
349
350
350
That's what `parseInt` and `parseFloat` are for.
351
351
352
-
They "read" numbers within a string and unless there's an error (for example, if the first character of a string is not a number), the first valid number is returned. The function `parseInt` returns an integer, whilst `parseFloat` will return a floating-point number:
352
+
They "read" a number from a string until they can. In case of an error, the gathered number is returned. The function `parseInt` returns an integer, whilst `parseFloat` will return a floating-point number:
353
353
354
354
```js run
355
355
alert( parseInt('100px') ); // 100
356
356
alert( parseFloat('12.5em') ); // 12.5
357
357
358
-
alert( parseInt('12.3') ); // 12, only the integer is returned
358
+
alert( parseInt('12.3') ); // 12, only the integer part is returned
359
359
alert( parseFloat('12.3.4') ); // 12.3, the second point stops the reading
360
360
```
361
361
@@ -423,7 +423,7 @@ For different numeral systems:
423
423
424
424
For converting values like `12pt` and `100px` to a number:
425
425
426
-
- Use `parseInt/parseFloat` for the "soft" conversion, which reads a number from a string until broken by something that is not a number.
426
+
- Use `parseInt/parseFloat` for the "soft" conversion, which reads a number from a string and then returns the value they could read before the error.
0 commit comments