</>
Skip to content
JavaScript lessons (21/64)

JavaScript — Number Methods

Formatting for humans

const price = 1234.5;

price.toFixed(2);              // "1234.50"  ← STRING, always
price.toFixed(0);              // "1235"
(3.14159).toFixed(3);          // "3.142"

toFixed rounds then renders — the go-to for money display:

const total = 19.999;
console.log(`$${total.toFixed(2)}`);   // $20.00

Trap: it returns a string. total.toFixed(2) + 1 gives "20.001". Convert back with Number(...) if math must continue.

Locale-aware formatting — the upgrade

price.toLocaleString("en-US");    // "1,234.50"
price.toLocaleString("de-DE");    // "1.234,50"
price.toLocaleString("en-US", { style: "currency", currency: "USD" });
// "$1,234.50"

const big = 9876543210;
big.toLocaleString();             // "9,876,543,210"

Thousand separators and currency symbols handled per locale — no manual comma surgery.

Converting number → string

(42).toString();     // "42"
(42).toString(2);    // "101010" — binary! (radix argument)
(255).toString(16);  // "ff"     — hex
String(42) === (42).toString();   // true — same result, global fn style

String → number family

CallInputResultNotes
Number(s)"42" / "42x"42 / NaNstrict whole-string
parseInt(s)"42px" / "42.9"42 / 42leading digits only
parseFloat(s)"3.5em"3.5keeps decimals
+s"42"42unary plus = strict shortcut
parseInt("08");            // 8
parseInt("0x1F");          // 31 — auto-hex detection!
parseFloat("50%")          // 50 — ignores trailing junk
Number("")                 // 0  😐 quirk
Number(null)               // 0  quirk
Number(undefined)          // NaN

Useful static checks

Number.isInteger(10)      // true
Number.isInteger(10.5)    // false
Number.isFinite(1/0)      // false — Infinity rejected
Number.isNaN(NaN)         // true  — the safe NaN test
Number.MAX_SAFE_INTEGER   // 9007199254740991 — beyond this, use BigInt

Rounding beyond toFixed

Math.round(2.5)     // 3   half-up
Math.floor(-2.5)    // -3
Math.ceil(2.1)      // 3
// round to nearest 0.05 (pricing steps):
Math.round(7.23 * 20) / 20;   // 7.25

Chaining a real formatter

function formatEUR(n) {
    return n.toLocaleString("en-IE", { style: "currency", currency: "EUR" });
}
formatEUR(1234567.891);   // €1,234,567.89

Mini Practice

  1. Render five prices with toFixed(2); prove typeof stays string
  2. Localize one number in three locales via toLocaleString
  3. Parse "12kg", "12.7km", "x12" — match each to its parser
  4. Binary/hex round-trips: (255).toString(16) → back via parseInt(_,16)
  5. Build formatPercent(0.876) → "87.6%" using toFixed + concat

Next: random →

Related Topics

Frequently Asked Questions about Number Methods

What is Number Methods in JavaScript?

Number Methods is a fundamental concept in JavaScript. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Number Methods?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Number Methods.

Why is Number Methods important in JavaScript?

Number Methods is essential for JavaScript development. Understanding this concept will help you write better code and solve real-world problems more effectively.