> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/val/prim/num.md).

# Number

[JS](/web/js.md) ⟩ [value](/web/js/val.md) ⟩ [primitive](/web/js/val/prim.md) ⟩ number

{% hint style="success" %}
[**convert**](/web/js/val/type/convert.md) any [value](/web/js/val.md) 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](/web/js/val/prim/bigint.md) <mark style="color:red;">**can't**</mark>**&#x20;**<mark style="color:yellow;">**be**</mark> [**converted**](/web/js/val/type/convert.md) **to** <mark style="color:purple;">**Number**</mark>:exclamation:(:point\_right: [<mark style="color:red;">**TypeError**</mark>](/web/js/err/type/no-bigint-to-num.md) )
{% endhint %}

{% hint style="danger" %}
[arithmetic operator](/web/js/grammar/op/arithmetic.md)(s) <mark style="color:red;">**do not**</mark>**&#x20;**<mark style="color:yellow;">**allow**</mark> to <mark style="color:yellow;">**mix**</mark> [BigInt](/web/js/val/prim/bigint.md) 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>](/web/js/val/prim/num/floating-point.md).
* <mark style="color:red;">**all**</mark> [<mark style="color:blue;">**division**</mark>](/web/js/grammar/op/arithmetic/binary/division.md) have [<mark style="color:yellow;">**floating-point**</mark>](/web/js/val/prim/num/floating-point.md) results.&#x20;
  {% endhint %}

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

{% tab title="🔴 主題" %}

* <mark style="color:yellow;">**operations of numbers**</mark>
  * [arithmetic operator](/web/js/grammar/op/arithmetic.md)
* <mark style="color:yellow;">**characteristics of numbers**</mark>
  * :beginner:[floating-point](/web/js/val/prim/num/floating-point.md) - implementation of JS numbers.
* <mark style="color:yellow;">**number functions**</mark>
  * [n.isEqual()](/web/js/val/prim/num/number+ext/n.isequal.md)
  * [isNumber()](/web/js/val/prim/num/number+ext/isnumber.md) 🚧
* [<mark style="color:yellow;">**special numbers**</mark>](/web/js/val/prim/num/special.md)
  * [Infinity](/web/js/val/prim/num/special/infinity.md) <mark style="color:yellow;">**/**</mark> [NaN](/web/js/val/prim/num/special/nan.md)
  * [Number.EPSILON](/web/js/val/prim/num/special/number.epsilon.md)
  * [Number.MAX\_VALUE](/web/js/val/prim/num/special/number.max_value.md)
  * [Number.MAX\_SAFE\_INTEGER](/web/js/val/prim/num/special/number.max_safe_integer.md)
  * [Random](/web/js/val/prim/num/random.md)
  * [Quaternion](/web/js/val/prim/num/quaternion.md)
    {% endtab %}

{% tab title="🔸 屬性" %}

* [n.isEqual()](/web/js/val/prim/num/number+ext/n.isequal.md)
* [isNumber()](/web/js/val/prim/num/number+ext/isnumber.md)
  {% 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 %}
