# Number

[JS](../..) ⟩ [value](..) ⟩ [primitive]() ⟩ number

{% hint style="success" %}
[**convert**](../type/convert) any [..](.. "mention") to <mark style="color:purple;">**Number**</mark>.

```javascript
+x               // any -> number, same as `Number(x)`
+x.toFixed(2)    // number -> at most 2 decimal places
```
{% endhint %}

{% tabs %}
{% tab title="🧨 雷區" %}
{% hint style="danger" %}
[bigint](bigint "mention") <mark style="color:red;">**can't**</mark>**&#x20;**<mark style="color:yellow;">**be**</mark> [**converted**](../type/convert) **to** <mark style="color:purple;">**Number**</mark>:exclamation:(:point\_right: [<mark style="color:red;">**TypeError**</mark>](../../err/type/no-bigint-to-num) )
{% endhint %}

{% hint style="danger" %}
[arithmetic](../../grammar/op/arithmetic "mention")(s) <mark style="color:red;">**do not**</mark>**&#x20;**<mark style="color:yellow;">**allow**</mark> to <mark style="color:yellow;">**mix**</mark> [bigint](bigint "mention") and <mark style="color:purple;">**Number**</mark>:exclamation:
{% endhint %}

{% hint style="danger" %}
Be care with <mark style="color:yellow;">**very small**</mark> or <mark style="color:yellow;">**very big**</mark> numbers:exclamation:

```javascript
const e = Number.EPSILON;         // 2^(-52)    (very small)
const BIG = Math.pow(10, 300);    // 10^300     (very big)

Number.EPSILON,               //  2.220446049250313e-16 = 2^(-52)
(0.1 + 0.2) - 0.3,            // ❗️ 5.551115123125783e-17 < e
0.1 + 0.2 === 0.3,            // ❗️ false  (should be true)
2.5 + e > 2.5,                // ❗️ false  (should be true)
BIG + 1 > BIG,                // ❗️ false  (should be true)   
BIG + 1 === BIG,              // ❗️ true   (should be false)
```
{% endhint %}
{% endtab %}

{% tab title="⭐️ 重點" %}
{% hint style="success" %}
* <mark style="color:red;">**all**</mark> <mark style="color:purple;">**numbers**</mark> are [<mark style="color:yellow;">**floating-point**</mark>](num/floating-point).
* <mark style="color:red;">**all**</mark> [<mark style="color:blue;">**division**</mark>](../../grammar/op/arithmetic/binary/division) have [<mark style="color:yellow;">**floating-point**</mark>](num/floating-point) results.&#x20;
{% endhint %}

{% hint style="info" %}
[compare](../../grammar/op/relational/compare "mention")(s) <mark style="color:red;">**do**</mark>**&#x20;**<mark style="color:yellow;">**allow**</mark> to <mark style="color:yellow;">**mix**</mark> [bigint](bigint "mention") and <mark style="color:purple;">**Number**</mark>:exclamation:
{% endhint %}
{% endtab %}

{% tab title="🔴 主題" %}
* <mark style="color:yellow;">**operations of numbers**</mark>
  * [arithmetic](../../grammar/op/arithmetic "mention")
* <mark style="color:yellow;">**characteristics of numbers**</mark>
  * :beginner:[floating-point](num/floating-point "mention") - implementation of JS numbers.
* <mark style="color:yellow;">**number functions**</mark>
  * [n.isequal](num/number+ext/n.isequal "mention")
  * [isnumber](num/number+ext/isnumber "mention") 🚧
* [<mark style="color:yellow;">**special numbers**</mark>](num/special)
  * [infinity](num/special/infinity "mention") <mark style="color:yellow;">**/**</mark> [nan](num/special/nan "mention")
  * [number.epsilon](num/special/number.epsilon "mention")
  * [number.max\_value](num/special/number.max_value "mention")
  * [number.max\_safe\_integer](num/special/number.max_safe_integer "mention")
  * [random](num/random "mention")
  * [quaternion](num/quaternion "mention")
{% endtab %}

{% tab title="🔸 屬性" %}
* [n.isequal](num/number+ext/n.isequal "mention")
* [isnumber](num/number+ext/isnumber "mention")
{% endtab %}

{% tab title="📘 手冊" %}
* [Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)
* [Intl.NumberFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat)
{% endtab %}

{% tab title="🗣 討論" %}
* [How to round to at most 2 decimal places, if necessary](https://stackoverflow.com/a/28728643/7185426)
{% endtab %}
{% endtabs %}
