convert fractions into decimal

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gubbachchi
    New Member
    • Jan 2008
    • 59

    convert fractions into decimal

    Hi all,

    How to convert the fractions into decimals and vice versa in php.
    I have a form, where the user will enter fractions in the text boxes such as "1 1/2", "1 1/4" and so on. I need to store all these values in mysql database after converting it into decimal value, and again fetch the value from database and re-convert it into fractions and display it to the user in a different php page. So what is best solution for this.
    Can anyone give some hints.


    With regards,
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    Originally posted by gubbachchi
    Hi all,

    How to convert the fractions into decimals and vice versa in php.
    I have a form, where the user will enter fractions in the text boxes such as "1 1/2", "1 1/4" and so on. I need to store all these values in mysql database after converting it into decimal value, and again fetch the value from database and re-convert it into fractions and display it to the user in a different php page. So what is best solution for this.
    Can anyone give some hints.


    With regards,

    To my knowledge you can't do it easily without a reasonabley big program, and even then, there will be lots of fractions it won't do. How about storing the input as a string in the database, and then just calculated it on the fly everytime it is called? The problem is if you want the answer as a fraction.

    Maybe try and google the programming for how a scientific calculator does it. If you can find the logical expression it should be easy, but I can't think of it.

    Comment

    • TheServant
      Recognized Expert Top Contributor
      • Feb 2008
      • 1168

      #3
      Actually, look at this from Zend Tech itself!

      If that doesn't help this has been discussed and tested here, so have a read and come back with any more questions. I am curious to see how you go, so if you find a way (either using those links or not) please post your code so it can help future users.

      TS

      Comment

      • dlite922
        Recognized Expert Top Contributor
        • Dec 2007
        • 1586

        #4
        Originally posted by gubbachchi
        Hi all,

        How to convert the fractions into decimals and vice versa in php.
        I have a form, where the user will enter fractions in the text boxes such as "1 1/2", "1 1/4" and so on. I need to store all these values in mysql database after converting it into decimal value, and again fetch the value from database and re-convert it into fractions and display it to the user in a different php page. So what is best solution for this.
        Can anyone give some hints.


        With regards,
        Don't know WHY you're doing this conversion if there's no calculations on it, just store it as a string and call it back! If you are doing calculations...

        Well The servant posted some info, but i'd like to attack this problem the old fashion way.

        For example I get the string "1 1/4" and want 1.25 stored in DB.

        You explode() on space. You'll have "1" and "1/4" in an array.

        Then explode() "1/4" on "/" OR "\" (or both to be safe?).

        Then you'll have 3 numbers 1, 1, and 4.

        You get float value of 1/4, which should give you 0.25 in a variable. then add the 1 left over, and you'll have 1.25.

        (I should have given you the code vs logic, must faster, but alas, you learn as you do)

        for converting back. You explode() on what???? if you said period "." you are Correct!

        you'll have "1", and "25" in an array. THEN you must find out how many characters in this second string, in this case 2.

        If its 1, you must put it over 10, if its 2, you must divide (denomenator) is 100.

        In this case it would be 100. Logically now we have "1 25/100".... almost there but not quite.

        We find the greatest common divisor using the handy dandy php function gmp_gcd("25","1 00"), which should output 25!

        Dividie both numerator and denomeator by that value and should get an integer.

        What integers!??? 1 and 4 silly!!

        There you have it, 1, 1, 4, which you could display as "1 1/4" in a string!

        VOILA!

        basic freshman math!

        Cheerio!


        DM

        Comment

        • TheServant
          Recognized Expert Top Contributor
          • Feb 2008
          • 1168

          #5
          Yes, that is the best way to do it if you are not planning on calculating anything. I just presumed you were ;)

          Comment

          • gubbachchi
            New Member
            • Jan 2008
            • 59

            #6
            Thank you all,

            I will go through your suggestions and choose the best way.

            Comment

            Working...