LESSON 07 · BEGINNER4 min read

Numbers, Math & Precision

Use JavaScript's single number type, round and format results deliberately, and detect invalid numeric input.

ES2024 / Node.js 20+Free to readModule 2 of 19
YOUR NEXT SMALL WINS

By the end, you’ll be able to…

  • Explain how JavaScript stores numbers and why some decimals are imprecise.
  • Use arithmetic and the Math helpers for common calculations.
  • Format values with toFixed and parse numeric text with parseInt or parseFloat.
  • Detect NaN with Number.isNaN and choose when to round.

01One number type

JavaScript has one number type for both integers and decimals. Values are stored as IEEE 754 double-precision floating-point numbers. That design handles an enormous range but cannot represent every decimal fraction exactly. As a result, 0.1 + 0.2 produces 0.30000000000000004 rather than 0.3.

The same type also represents Infinity and NaN, which means not a number. NaN appears when a numeric operation has no meaningful result, such as 0 / 0 or Number("four"). Test for it with Number.isNaN, not with equality, because NaN === NaN is false. For integers beyond Number.MAX_SAFE_INTEGER, use BigInt.

number-limits.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

02Arithmetic and readable steps

Arithmetic uses +, -, *, /, and %, with ** for exponentiation. Multiplication and division bind more tightly than addition and subtraction, so parentheses are your tool for stating intent. The remainder operator is useful for grouping and even or odd checks: 17 % 5 is 2.

Keep units visible in variable names. A subtotal in dollars and a quantity in items should not be mixed accidentally. When a calculation has several steps, name the intermediate results. That makes a wrong number easy to trace to the step that produced it.

order-total.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

03Rounding and display formatting

Math.round rounds to the nearest integer, Math.floor always moves down, and Math.ceil always moves up. Math.abs removes the sign, while Math.min and Math.max choose an extreme. These functions return numbers; they do not format them for display.

toFixed converts a number to a string with a requested number of decimal places. It is a display tool, not a precision fix. The underlying binary value may already be slightly off, and toFixed follows its own rounding rules. Round at the boundary where a person reads the value, not in the middle of a calculation.

rounding.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

04Parsing text and detecting NaN

Number converts a whole value strictly: Number("42") is 42, Number("") is 0, and Number("42px") is NaN. parseInt and parseFloat read a numeric prefix and stop at the first character that cannot continue the number. parseInt also takes a radix; pass 10 unless you intentionally need another base.

Choose the tool that matches the input contract. A field that should contain only a number is better handled with Number, because trailing text becomes an error you can detect. A label such as 24.75 USD needs parseFloat. Never let a silent partial parse turn malformed data into a plausible number.

parse-numbers.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

05Choose precision deliberately

Money is the classic precision case. Store cents as integers, do arithmetic on those integers, and divide by 100 only when displaying. This avoids accumulating tiny errors across many additions. It also makes the rounding decision explicit instead of accidental.

When comparing decimals, compare a rounded display value or check that the difference is below a small tolerance. Do not expect two floating-point calculations to be exactly equal just because a hand calculation says they should be. For ordinary lesson output, rounding at the end is enough.

money.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
LET IT CLICK

The ideas worth keeping.

  • JavaScript uses one floating-point number type, so some decimal results are approximate.
  • Math helpers return numbers, while toFixed returns a formatted string.
  • Parse input deliberately, detect NaN with Number.isNaN, and round only where the value is displayed.
TURN UNDERSTANDING INTO SOMETHING YOU CAN DO

Make it yours.

Type and test your code right here in the interactive workspace. Run your solution, check the output, and compare with the worked guide when ready.

01

Exercise 1: Produce a rounding report

For a = 7.8 and b = -2.3, print the floor, ceiling, and rounded value of each number on separate lines. Then print the absolute value of b.

your_turn_1.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
Explore a worked solution
one_possible_solution.jsJavaScript
const a = 7.8;const b = -2.3;console.log(Math.floor(a), Math.ceil(a), Math.round(a));console.log(Math.floor(b), Math.ceil(b), Math.round(b));console.log(Math.abs(b));

The output is 7 8 8, then -3 -2 -2, then 2.3. Floor always moves down, which is why the negative value becomes -3, while round moves to the nearest integer.

02

Exercise 2: Parse a labeled price

Given label = "24.75 USD", extract the numeric value with parseFloat, add 5 for shipping, and print the total with two decimal places.

your_turn_2.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
Explore a worked solution
one_possible_solution.jsJavaScript
const label = "24.75 USD";const price = parseFloat(label);const total = price + 5;console.log(total.toFixed(2));

parseFloat reads 24.75 and stops at the space. Adding 5 produces 29.75, and toFixed(2) formats that number as the string 29.75.

A SMALL QUESTION. A USEFUL NEXT STEP.

What stayed with you?

Check the ideas while they’re fresh. Your answers stay in this page and feedback appears as you go.

A LITTLE ACTIVE RECALL0 / 3 answered
01Why is 0.1 + 0.2 === 0.3 false?
02What does parseInt("42px", 10) return?
03What does toFixed return?
⚡CODING CHALLENGE
🔒Required to Mark Lesson Complete

Exercise 1: Produce a rounding report

For a = 7.8 and b = -2.3, print the floor, ceiling, and rounded value of each number on separate lines. Then print the absolute value of b.

EXPECTED OUTPUT
7 8 8
-3 -2 -2
2.3
main.jsJavaScript
Ctrl + ↵
Execution Console
Click "Run & Verify" (or press Ctrl + Enter) to test your code against the challenge.

Keep exploring.

The official references behind this lesson, when you want a second angle or the full details.

One lesson closer.

Reading is a start. Trying it yourself makes the connection.

Solve the hands-on coding challenge in the knowledge section to verify your code and unlock lesson completion.