Borislav Hadzhiev
Reading timeยท2 min
Photo from Unsplash
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 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.