Convert a decimal number into a fraction

The title pretty much says it all: this JavaScript code takes a decimal number and converts it into a fraction.

This code:

  • recognizes 0.3333333 as 1/3
  • allows not to extract the integral part: 1.5 can return 1 1/2 (by default) or 3/2
  • works with negative numbers

A few examples:

Please let me know in the comments below if there’s anything that can be improved, or simply if and where you use it. I’d very much appreciate to know who likes it!

Leave a comment