Technical skill practice

JavaScript toFixed: Format a Number and Inspect Rounding Limits

Use toFixed when you want a fixed number of digits after the decimal point in a formatted result. Its result is text. Formatting a Number does not turn binary floating-point arithmetic into exact decimal arithmetic.

The task

Prepare two display labels and inspect a value near a decimal rounding boundary. Keep formatting separate from later calculations.

Runnable Node 22 example

const assert = require('node:assert/strict');

const displayed = (7.5).toFixed(2);
assert.equal(displayed, '7.50');
assert.equal(typeof displayed, 'string');
assert.equal((2.55).toFixed(1), '2.5');
assert.equal(displayed + 1, '7.501');
assert.equal(Number(displayed) + 1, 8.5);
assert.throws(() => (1).toFixed(101), RangeError);
console.log(`formatted=${displayed}; boundary=${(2.55).toFixed(1)}`);

Expected output and explanation

The first result keeps two fractional digits, including a trailing zero. Adding one to that result performs string concatenation, which explains 7.501 rather than a numeric increment. Explicit conversion changes the operation but does not recover any precision previously lost.

The 2.55 example rounds as the actual stored Number dictates. It warns against assuming that every decimal-looking input is represented exactly. The digit-range assertion also demonstrates a rejected formatting request in Node22.

Output from the example

formatted=7.50; boundary=2.5

Pitfall to check

Do not use toFixed as evidence that a financial calculation is exact. Choose a numeric representation and rounding policy appropriate to that application. This fixture illustrates display formatting of ordinary measurements only.

Questions, answered

Does toFixed change the original number?

No. It returns a string representation at the requested precision. Keep the numeric value separately if later arithmetic still needs it.

Sources and examples

ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.

Your experience. A complete resume.

Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.

Free account required. Free-plan PDF exports include a watermark.