How to get a fraction portion of a float number with JavaScript?

Sometimes, we want to get a fraction portion of a float number with JavaScript.

In this article, we’ll look at how to get a fraction portion of a float number with JavaScript.

How to get a fraction portion of a float number with JavaScript?

To get a fraction portion of a float number with JavaScript, we can use the % operator to get the remainder of the number divided by 1.

For instance, we write:

const frac = (f) => {
  return f % 1;
}
console.log(frac(123.45))

to define the frac function that takes the number f.

And we return the remainder of f divided by 1 with f % 1.

Therefore, the console log should log 0.45000000000000284.

Conclusion

To get a fraction portion of a float number with JavaScript, we can use the % operator to get the remainder of the number divided by 1.