--- id: javascript-number-methods title: "JavaScript Number Methods" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["number methods", "toFixed", "toPrecision", "Number()", "parseInt", "parseFloat"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.89 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["javascript", "js", "web", "frontend", "w3schools", "numbers", "number-methods", "parsing"] raw_sources: ["https://www.w3schools.com/js/js_number_methods.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Number Methods]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Basic number methods (`toString`, `toExponential`, `toFixed`, `toPrecision`, `valueOf`) work on any number, while static `Number.*` methods (`isInteger`, `isFinite`, `isNaN`, `isSafeInteger`, `parseInt`, `parseFloat`) can only be used on `Number` itself. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Basic methods work on any number** β€” `toString()`, `toExponential()`, `toFixed()`, `toPrecision()`, `valueOf()`. [S1] - **Static methods only on `Number`** β€” `Number.isFinite()`, `Number.isInteger()`, `Number.isNaN()`, `Number.isSafeInteger()`, `Number.parseInt()`, `Number.parseFloat()`. [S1] - **`toString()`** β€” returns a number as a string; accepts a radix. [S1] - **`toFixed()`** β€” returns a string with a specified number of decimals; perfect for working with money. [S1] - **`toPrecision()`** β€” returns a string with a number written to a specified length. [S1] - **Conversion functions** β€” `Number()`, `parseInt()`, `parseFloat()` convert variables to numbers; `NaN` is returned when conversion fails. [S1] - **Methods cannot be used on variables for statics** β€” `Number.isInteger()` etc. must be called on `Number`; calling on a variable throws a TypeError. [S1] - **Safe integers** β€” range is -(2⁡³ - 1) to +(2⁡³ - 1); `9007199254740991` is safe, `9007199254740992` is not. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Format money** β€” `value.toFixed(2)` to render two decimal places. [S1] - **Parse leading number from text** β€” `parseInt("10 years")` / `parseFloat("10.33")` extract a number from the front of a string. [S1] - **Validate before use** β€” `Number.isInteger()` / `Number.isSafeInteger()` / `Number.isNaN()` to check values. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **Basic Methods** can be used on any number: `toString()`, `toExponential()`, `toFixed()`, `toPrecision()`, `valueOf()`. **Static Methods** can only be used on `Number`: `Number.isFinite()`, `Number.isInteger()`, `Number.isNaN()`, `Number.isSafeInteger()`, `Number.parseInt()`, `Number.parseFloat()`. [S1] **toString()** β€” returns a number as a string; can take a radix. [S1] ```javascript let x = 123; x.toString(); (123).toString(); (100 + 23).toString(); ``` ```javascript let x = 123; let text = x.toString(2); ``` **toExponential()** β€” returns a string with a number rounded and written using exponential notation. [S1] ```javascript let x = 9.656; x.toExponential(2); x.toExponential(4); x.toExponential(6); ``` **toFixed()** β€” returns a string with the number written with a specified number of decimals; perfect for working with money. [S1] ```javascript let x = 9.656; x.toFixed(0); x.toFixed(2); x.toFixed(4); x.toFixed(6); ``` **toPrecision()** β€” returns a string with a number written with a specified length. [S1] ```javascript let x = 9.656; x.toPrecision(); x.toPrecision(2); x.toPrecision(4); x.toPrecision(6); ``` **valueOf()** β€” returns a number as a number; used internally in JavaScript to convert Number objects to primitive values. [S1] ```javascript let x = 123; x.valueOf(); (123).valueOf(); (100 + 23).valueOf(); ``` **Converting Variables to Numbers** β€” three methods are available: [S1] | Method | Description | |--------|-------------| | Number() | Converts argument to number | | parseFloat() | Parses argument, returns floating point | | parseInt() | Parses argument, returns whole number | **Number()** β€” converts its argument to a number. [S1] ```javascript Number(true); Number(false); Number("10"); Number(" 10"); Number("10 "); Number(" 10 "); Number("10.33"); Number("10,33"); Number("10 33"); Number("John"); ``` **Number() Used on Dates** β€” converts a date to milliseconds. [S1] ```javascript Number(new Date("1970-01-01")) ``` ```javascript Number(new Date("1970-01-02")) ``` ```javascript Number(new Date("2017-09-30")) ``` **parseInt()** β€” parses and returns a whole number. [S1] ```javascript parseInt("-10"); parseInt("-10.33"); parseInt("10"); parseInt("10.33"); parseInt("10 20 30"); parseInt("10 years"); parseInt("years 10"); ``` **parseFloat()** β€” parses and returns a floating point number. [S1] ```javascript parseFloat("10"); parseFloat("10.33"); parseFloat("10 20 30"); parseFloat("10 years"); parseFloat("years 10"); ``` **Number Object Methods** (static): [S1] | Method | Description | |--------|-------------| | Number.isInteger() | Returns true if argument is integer | | Number.isNaN() | Returns true if argument is NaN | | Number.isFinite() | Returns true if not Infinity or NaN | | Number.isSafeInteger() | Returns true if safe integer | | Number.parseFloat() | Converts string to number | | Number.parseInt() | Converts string to whole number | Number methods can only be accessed like `Number.isInteger()`; calling them on a variable produces a TypeError ("X.isInteger is not a function"). `Number.isNaN()` is the preferred way to check for equality with NaN. [S1] **Number.isInteger()** [S1] ```javascript Number.isInteger(10); Number.isInteger(10.5); ``` **Number.isFinite()** [S1] ```javascript Number.isFinite(123); ``` **Number.isNaN()** [S1] ```javascript Number.isNaN(123); ``` **Number.isSafeInteger()** β€” safe integer range is -(2⁡³ - 1) to +(2⁡³ - 1). [S1] ```javascript Number.isSafeInteger(10); Number.isSafeInteger(12345678901234567890); ``` **Number.parseFloat()** [S1] ```javascript Number.parseFloat("10"); Number.parseFloat("10.33"); Number.parseFloat("10 20 30"); Number.parseFloat("10 years"); Number.parseFloat("years 10"); ``` **Number.parseInt()** [S1] ```javascript Number.parseInt("-10"); Number.parseInt("-10.33"); Number.parseInt("10"); Number.parseInt("10.33"); Number.parseInt("10 20 30"); Number.parseInt("10 years"); Number.parseInt("years 10"); ``` ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's own snippets are the canonical applied examples β€” formatting `9.656` with `toFixed`/`toPrecision`/`toExponential`, converting strings and dates with `Number()`, parsing with `parseInt`/`parseFloat`, and validating with the static `Number.*` methods. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Format for money: ```javascript let x = 9.656; x.toFixed(2); ``` Parse a number from text: ```javascript parseInt("10 years"); parseFloat("10.33"); ``` Validate a value: ```javascript Number.isInteger(10); Number.isSafeInteger(9007199254740991); ``` ## βš–οΈ 비ꡐ 및 선택 κΈ°μ€€ (Comparison & decision criteria) - **`Number()` vs `parseInt()` vs `parseFloat()`** β€” `Number()` converts the whole argument (returns NaN if it is not fully numeric, e.g. `"10 33"`); `parseInt()` returns a whole number from the leading numeric portion; `parseFloat()` returns a floating point from the leading numeric portion. [S1] - **Basic vs static methods** β€” basic methods (`toFixed`, etc.) are called on a number value; static methods (`Number.isInteger`, etc.) must be called on `Number` itself or they throw a TypeError. [S1] - **`Number.isNaN()` is preferred** for checking equality with NaN. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.89 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[JavaScript Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[JavaScript Numbers]], [[JavaScript Number Properties]], [[JavaScript Data Types]], [[JavaScript Operators]] - **μ°Έμ‘° λ§₯락:** Referenced whenever formatting numbers for display or converting strings/dates into numeric values. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript Number Methods β€” https://www.w3schools.com/js/js_number_methods.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Number Methods" page (Astra wiki-curation, P-Reinforce v3.1 format).