Last updated: Mar 3, 2024
Reading timeยท3 min
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.
// โ 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.
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.
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:
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.
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
.
console.log(3.0000 === 3); // ๐๏ธ true
Adding or removing insignificant trailing zeros doesn't change the number.
If you have to remove the trailing zeros from a number, define a reusable function.
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 then formats it to N decimal places.
You can also remove the trailing zeros from a number by multiplying the number
by 1
.
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.
You can also remove the trailing zeros from a number by converting it to a string.
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.
You can also use the String.replace()
method to remove the trailing zeros from
a number.
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.
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:
Name | Description |
---|---|
pattern | The pattern to look for in the string. Can be a string or a regular expression. |
replacement | A string used to replace the substring match by the supplied pattern. |
The first argument we passed to the replace()
method is a regular expression.
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.
You can learn more about the related topics by checking out the following tutorials: