Remove the Trailing Zeros from a Number in JavaScript

avatar

Borislav Hadzhiev

2 min

banner

Photo from Unsplash

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

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

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"

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

The function takes a number (or a string), removes the trailing zeros from the number and 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.

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 ยฉ 2023 Borislav Hadzhiev