sajad torkamani

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

Leave a comment

Your email address will not be published. Required fields are marked *