Back to JavaScript Notes
Topic #300

Number Methods

By the end of this lesson, you will be able to format JavaScript numbers for display and convert numeric strings into numbers using methods and helpers such as toFixed and parseInt.

What it is

Number methods are functions that let you turn a number into a formatted string or turn a string into a number. In JavaScript, a primitive number such as 3.14 can call methods like toFixed() because the engine temporarily treats it as a Number object.

toFixed() returns a string with a fixed number of decimal places. parseInt() is a global conversion function, not a method on Number.prototype, but it is commonly used with number formatting and parsing. Related terms include toString(), toPrecision(), toExponential(), Number(), parseFloat(), radix, and NaN.

Why it matters

  • It lets you display prices, percentages, and measurements with consistent decimal places.
  • It helps convert user input from text fields into usable numbers.
  • It prevents accidental string math, such as "19.99" + 5 becoming "19.995".
  • It gives you control over rounding and precision for simple output.
  • It helps extract integers from mixed text, such as years or IDs.

Syntax or steps

const display = value.toFixed(2);
const integer = parseInt(input, 10);
const numeric = Number(input);

(3.14159).toFixed(2); // "3.14"

Use toFixed(digits) when you have a number and want a string with a set number of decimal places. Use parseInt(string, radix) when you want the leading integer from a string. Use Number(string) when the whole string should become a number.

Example

const price = 19.99;
const taxRate = 0.08;

const priceText = price.toFixed(2);
const taxPercent = (taxRate * 100).toFixed(1);

const input = "  42.75 dollars";
const whole = parseInt(input, 10);
const exact = Number("42.75");
const invalid = Number(input);

console.log(priceText);
console.log(taxPercent);
console.log(whole);
console.log(exact);
console.log(invalid);
console.log(typeof priceText);

price.toFixed(2) formats 19.99 as the string "19.99". (taxRate * 100).toFixed(1) formats the percentage as "8.0". parseInt(input, 10) reads the leading integer 42 and ignores the rest of the text. Number("42.75") converts the full numeric string to the number 42.75. Number(input) returns NaN because the string is not entirely numeric. typeof priceText is "string", not "number".

Common mistakes

  • Forgetting that toFixed() returns a string. If you need to do math, convert it back with Number() or parseFloat().
  • Omitting the radix in parseInt(). Always pass 10 for decimal integers to avoid unexpected behavior.
  • Expecting parseInt() to round decimals. parseInt("0.9", 10) returns 0, not 1.
  • Using toFixed() for exact money calculations. JavaScript numbers are binary floating-point values, so rounding can be surprising.

When to use it

Goal Use Closest alternative When to choose the alternative
Display a number with fixed decimal places toFixed() Intl.NumberFormat Use Intl.NumberFormat for locale-aware grouping, currency symbols, or more predictable display formatting.
Convert a string to a number parseInt() for integers Number() or parseFloat() Use Number() when the whole string must be numeric; use parseFloat() when you need a decimal value.

Practice

Guided exercise: Format 7.5 as a two-decimal string and convert "123abc" to an integer.

Expected result: "7.50" and 123.

Challenge: What does parseInt("99.999", 10) return, and how would you get 99.999 as a number?

Solution hint: parseInt() truncates at the decimal point, so it returns 99. Use Number("99.999") or parseFloat("99.999") to get 99.999.

Quick check

Question: What does (3.14159).toFixed(2) return?

Answer: It returns the string "3.14".

Summary

Number formatting and conversion are about moving between display text and numeric values. toFixed() is useful for simple decimal display, while parseInt() and Number() help convert strings into numbers.

Always remember the return type: formatting methods usually return strings, and parsing helpers return numbers or NaN.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Number Methods – FAQs

Quick answers about learning Number Methods in JavaScript.

This free note from Coding Now Tech Institute explains Number Methods in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including Number Methods, is 100% free with no signup required.
With focused practice, most students grasp Number Methods in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now