Remove the Trailing Zeros from a Number in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
3 min

banner

# Remove the Trailing Zeros from a Number in JavaScript

Use the parseFloat() function to remove the trailing zeros from a number.

The parseFloat() function parses the provided value, returning a floating point number while removing all the trailing zeros.

index.js
// โœ… remove trailing zeros from a number const num1 = 13.4530000; const result1 = parseFloat(num1); console.log(result1); // ๐Ÿ‘‰๏ธ 13.453 // --------------------------- // โœ… parse a string to a number and remove trailing zeros const str1 = '13.453000'; const result2 = parseFloat(str1); console.log(result2); // ๐Ÿ‘‰๏ธ 13.453

remove trailing zeros from number

The code for this article is available on GitHub

We used the parseFloat() function to strip all unnecessary trailing zeros from a number.

The only parameter the function takes is a value to parse to a floating-point number.

This approach also works with numbers that are wrapped in a string.

index.js
const str1 = '13.453000'; const result2 = parseFloat(str1); console.log(result2); // ๐Ÿ‘‰๏ธ 13.453

# Removing the trailing zeros from a number and format to N decimals

If you need to format the number to N decimal places, use the toFixed() method.

index.js
const num1 = 13.4534567000; const result1 = num1.toFixed(3); console.log(result1); // ๐Ÿ‘‰๏ธ "13.453" // --------------------------- const str1 = '13.4534567000'; const result2 = parseFloat(str1).toFixed(3); console.log(result2); // ๐Ÿ‘‰๏ธ "13.453"

removing the trailing zeros from number and format to n decimals

The code for this article is available on GitHub

The Number.toFixed() method formats the number to the specified number of digits after the decimal and returns the result as a string.

The toFixed() method also:

  • rounds the number if necessary
  • pads the fractional part with zeros if you've specified more decimal places than the number has
index.js
console.log((5).toFixed(2)); // ๐Ÿ‘‰๏ธ '5.00' console.log((5.346).toFixed(2)); // ๐Ÿ‘‰๏ธ '5.35'

The toFixed() method returns a string. If you want to get the result as a number, pass it to the parseFloat() function.

index.js
const num1 = 13.45345670000; const result1 = parseFloat(num1.toFixed(3)); console.log(result1); // ๐Ÿ‘‰๏ธ 13.453 console.log(typeof result1); // ๐Ÿ‘‰๏ธ number

Once a value is converted to a number in JavaScript, all insignificant trailing zeros get automatically dropped.

The language doesn't keep trailing zeros around because the number 3.00 is equal to 3.

index.js
console.log(3.0000 === 3); // ๐Ÿ‘‰๏ธ true

Adding or removing insignificant trailing zeros doesn't change the number.

# Creating a reusable function

If you have to remove the trailing zeros from a number, define a reusable function.

index.js
function removeTrailingZeros(num, decimals) { const number = parseFloat(num) return parseFloat(number.toFixed(decimals)) } console.log(removeTrailingZeros(4.0000123, 2)) // ๐Ÿ‘‰๏ธ 4 console.log(removeTrailingZeros(4.3000000, 2)) // ๐Ÿ‘‰๏ธ 4.3 console.log(removeTrailingZeros('4.3000000', 2)) // ๐Ÿ‘‰๏ธ 4.3 console.log(removeTrailingZeros('4.0000123', 2)) // ๐Ÿ‘‰๏ธ 4 console.log(removeTrailingZeros('4.56780000', 3)) // ๐Ÿ‘‰๏ธ 4.568

creating reusable function

The code for this article is available on GitHub

The function takes a number (or a string), removes the trailing zeros from the number and then formats it to N decimal places.

# Remove the Trailing Zeros from a Number by multiplying by 1

You can also remove the trailing zeros from a number by multiplying the number by 1.

index.js
function removeTrailingZeros(num) { const number = parseFloat(num) return number * 1 } console.log(removeTrailingZeros(4.3000000, 2)) // ๐Ÿ‘‰๏ธ 4.3 console.log(removeTrailingZeros(4.0000123, 2)) // ๐Ÿ‘‰๏ธ 4.0000123 console.log(removeTrailingZeros('4.3000000', 2)) // ๐Ÿ‘‰๏ธ 4.3 console.log(removeTrailingZeros('4.0000123', 2)) // ๐Ÿ‘‰๏ธ 4.0000123 console.log(removeTrailingZeros('4.56780000', 3)) // ๐Ÿ‘‰๏ธ 4.5678

The function takes a number (or a string) as a parameter and multiplies the number by 1 which automatically drops all insignificant trailing zeros.

# Remove the Trailing Zeros from a Number by converting it to a String

You can also remove the trailing zeros from a number by converting it to a string.

index.js
const num = 13.45; const str = num.toString(); console.log(str); // ๐Ÿ‘‰๏ธ 13.45
The code for this article is available on GitHub

All trailing zeros get automatically dropped when the number gets converted to a string.

# Remove the Trailing Zeros from a Number by using replace()

You can also use the String.replace() method to remove the trailing zeros from a number.

index.js
function removeTrailingZeros(num) { const number = String(num).replace(/\.0+$/, ''); return parseFloat(number); } console.log(removeTrailingZeros(12.4500)); // ๐Ÿ‘‰๏ธ 12.45 console.log(removeTrailingZeros(12.00100)); // ๐Ÿ‘‰๏ธ 12.001 console.log(removeTrailingZeros(12.001001)); // ๐Ÿ‘‰๏ธ 12.001001

If you also want to format the number to N decimals, use the toFixed() method.

index.js
function removeTrailingZeros(num, decimals) { const number = String(num).replace(/\.0+$/, ''); return parseFloat(number).toFixed(decimals); } console.log(removeTrailingZeros(12.4560000, 2)); // ๐Ÿ‘‰๏ธ 12.46 console.log(removeTrailingZeros(12.123567000, 3)); // ๐Ÿ‘‰๏ธ 12.124
The code for this article is available on GitHub

The String.replace() method returns a new string with one, some, or all matches of a regular expression replaced with the provided replacement.

The method takes the following parameters:

NameDescription
patternThe pattern to look for in the string. Can be a string or a regular expression.
replacementA string used to replace the substring match by the supplied pattern.

The first argument we passed to the replace() method is a regular expression.

index.js
const number = String(num).replace(/\.0+$/, '');

The forward slashes / / mark the beginning and end of the regular expression.

We had to escape the period . with a backslash because periods have a special meaning in regular expressions.

The plus + matches the preceding item (the zero) 1 or more times.

The dollar $ sign matches the end of the input.

I've also written an article on how to remove the leading zeros from a number.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev