Fractional

Fractional is a library for parsing fractions.

Convert fractional string to float:

Fractional.to_f("1100 7/8") #=> 1100.875

Convert float to fractional string:

Fractional.to_s(1100.875)   #=> "1100 7/8"
Fractional.to_s(1100.875, :to_nearest => "1/2") #=> "1101"

Round a value to the nearest fraction:

Fractional.round_to_nearest_fraction("1100 1/7", "1/64")           #=> "1100 9/64"
Fractional.round_to_nearest_fraction(1100.142857142857143, "1/64") #=> 1100.140625

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Special thanks

Thanks to Jannis Harder for his hardcore float to rational method I nicked from markmail.org/message/nqgrsmaixwbrvsno

Copyright © 2009 Chris O’Sullivan. See LICENSE for details.