ProgramGuru

Numbers, Math and Rounding

Numbers, Math and Rounding

Ordinary JavaScript numbers use binary floating-point representation. Math.round rounds a numeric value, while toFixed formats a string for display; formatting does not change the stored number.

Before this topic, review Strings and Text Operations if its terms are unfamiliar.

What you’ll learn

  • Explain numbers, math and rounding through the school example.
  • Trace the shown operations and distinguish their result from displayed output.
  • Make the practice change and explain its consequence: The nearest integer is 75 and the one-decimal display is 75.2.

Work through the school example

This is JavaScript using language features and the host console. Read the initial values, then follow each operation to its displayed result. A declaration names a value, a function call performs an operation, and console.log displays its arguments.

JavaScript · school.js · complete example

const average = 72.56;
console.log(Math.round(average));
console.log(average.toFixed(1));

Expected output

73
72.6
Run in JavaScript compiler →

Recognize the important boundary

Not every decimal is represented exactly. Use a deliberate money policy, and validate safe integers when counts exceed ordinary application ranges.

Try a small variation

JavaScript · original source for practice

const average = 72.56;
console.log(Math.round(average));
console.log(average.toFixed(1));

Your task: Replace 72.56 with 75.24. Predict the result before running the edited program.

The source is repeated here. If the edit targets the HTML rather than the script, use the markup repeated below; the worked answer also repeats the changed markup.

Worked answer and reasoning

The nearest integer is 75 and the one-decimal display is 75.2.

JavaScript · complete worked variation

const average = 75.24;
console.log(Math.round(average));
console.log(average.toFixed(1));

Expected output

75
75.2
Run in JavaScript compiler →

Predict the original program

JavaScript · original program for this question

const average = 72.56;
console.log(Math.round(average));
console.log(average.toFixed(1));
What does the original JavaScript program display, in order?
Choose one answer. You can try again.

Answer and explanation

73 → 72.6

Ordinary JavaScript numbers use binary floating-point representation. Math.round rounds a numeric value, while toFixed formats a string for display; formatting does not change the stored number. Not every decimal is represented exactly. Use a deliberate money policy, and validate safe integers when counts exceed ordinary application ranges.

Keep the useful idea

Ordinary JavaScript numbers use binary floating-point representation. Math.round rounds a numeric value, while toFixed formats a string for display; formatting does not change the stored number.

Check your variation against this explanation: The nearest integer is 75 and the one-decimal display is 75.2.

Use the JavaScript array reference library for individual array operations and boundary tests to check a rule independently.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Truthiness and Missing Values →