PHP convert decimal into fraction and back?

Posted by John Isaacks on Stack Overflow See other posts from Stack Overflow or by John Isaacks
Published on 2009-12-23T17:01:19Z Indexed on 2010/04/05 14:53 UTC
Read the original article Hit count: 383

Filed under:
|
|
|

I want the user to be able to type in a fraction like:

 1/2
 2 1/4
 3

And convert it into its corresponding decimal, to be saved in MySQL, that way I can order by it and do other comparisons to it.

But I need to be able to convert the decimal back to a fraction when showing to the user

so basically i need a function that will convert fraction string to decimal:

fraction_to_decimal("2 1/4");// return 2.25

and a function that can convert a decimal to a faction string:

decimal_to_fraction(.5); // return "1/2"

How can I do this?

Thanks!

© Stack Overflow or respective owner

Related posts about php

Related posts about fractions