# Remove the Trailing Zeros from a Number in JavaScript

Last updated: Jan 2, 2023
3 min

## #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
```Copied!```// โ 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
```Copied!```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
```Copied!```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
```Copied!```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
```Copied!```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
```Copied!```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
```Copied!```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
```Copied!```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
```Copied!```const num = 13.45;

const str = num.toString();
console.log(str); // ๐๏ธ 13.45
``````

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
```Copied!```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
```Copied!```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 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
```Copied!```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.