Get decimal part of float
11 March 2022 (Updated 1 May 2022)
You want a function that takes a float with any number of decimal points, and returns the decimal part rounded to two decimal places:
decimalPart(45.7832333) // 0.78
decimalPart(12.01) // 0.01
Here’s how:
function decimalPart(num) {
const decimal = Math.abs(num % 1)
return parseFloat(decimal.toFixed(2))
}
Tagged:
Snippets
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment