Skip to content

How to Format a Number by Removing Trailing Zeros in JavaScript

Learn how to convert JavaScript numbers to clean strings without trailing zeros using String(), parse numeric strings with parseFloat(), and format decimals with toFixed() for display.

In JavaScript, the Number type itself does not store formatting information like trailing zeros. The number 12.500 is mathematically identical to and stored in the same way as 12.5. The concept of "removing trailing zeros" is actually a string formatting problem: how to convert a number into a string representation that omits any insignificant zeros after the decimal point.

This guide will clarify this important distinction and teach you the simplest and most effective methods for converting numbers into clean string formats, as well as parsing number-like strings.

Core Concept: Numbers vs. Their String Representation

It is crucial to understand that a Number in JavaScript is a pure mathematical value. It has no concept of "trailing zeros."

Problem (A Misconception)

// This number is stored in memory as just 12.5
const myNumber = 12.500; 

console.log(myNumber);        // Output: 12.5

// The trailing zeros are already "gone" because they are insignificant.
console.log(12.500 === 12.5); // Output: true

Solution: Converting a Number to a String

The simplest way to get a string representation of a number without unnecessary trailing zeros is to let JavaScript's default conversion handle it. You can do this by adding an empty string or by using the String() constructor.

Solution:

const num1 = 12.500;
const num2 = 10.00;

// Convert the numbers to their default string representation
const str1 = String(num1);
const str2 = String(num2);

console.log(str1); // Output: '12.5'
console.log(str2); // Output: '10'

Inverse Problem: Parsing a String into a Number

A more common real-world scenario is starting with a string that has trailing zeros (e.g., from an API or user input) and needing to convert it into a Number.

Problem: you have a string like "12.500" and want to convert it to the number 12.5.

Solution: Use the global parseFloat() function. This function parses a string argument and returns a floating-point number. It automatically discards any insignificant trailing zeros during the conversion.

const numberString = '12.500';

const number = parseFloat(numberString);

console.log(number); // Output: 12.5

Formatting to a Specific Number of Decimal Places

Sometimes, you need to format a number for display with a consistent number of decimal places, which might involve rounding or removing extra decimal places (but not just trailing zeros). The Number.prototype.toFixed() method is the right tool for this.

Problem: you have a number with many decimal places and want to display it neatly with only two.

// Problem: Display this number with only 2 decimal places.
const price = 19.99456;

Solution:

const price = 19.99456;

// .toFixed() rounds to the specified number of decimal places and returns a STRING.
const formattedPrice = price.toFixed(2);

console.log(formattedPrice);     // Output: '19.99'

const taxRate = 0.07000;
console.log(taxRate.toFixed(3)); // Output: '0.070'

Conclusion

The concept of "removing trailing zeros" in JavaScript is a matter of formatting a number into a string, not changing the number itself.

  • To get a clean string representation of a number without insignificant trailing zeros, the simplest method is to use the String() constructor: String(12.500) returns '12.5'.
  • To parse a string that contains trailing zeros into a proper number, the recommended method is parseFloat('12.500'), which returns the number 12.5.
  • To format a number to a fixed number of decimal places for display, use the number.toFixed(digits) method, which returns a formatted string.
Navigation