471,066 Members | 920 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 software developers and data experts.

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,
Apr 4 '08 #1
5 5261
TheServant
1,168 Expert 1GB
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.
Apr 4 '08 #2
TheServant
1,168 Expert 1GB
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
Apr 4 '08 #3
dlite922
1,584 Expert 1GB
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","100"), 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
Apr 4 '08 #4
TheServant
1,168 Expert 1GB
Yes, that is the best way to do it if you are not planning on calculating anything. I just presumed you were ;)
Apr 4 '08 #5
Thank you all,

I will go through your suggestions and choose the best way.
Apr 4 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by ferran | last post: by
33 posts views Thread by selowitch | last post: by
4 posts views Thread by Chris Dunaway | last post: by
15 posts views Thread by farah727rash | last post: by
32 posts views Thread by andresj | last post: by
4 posts views Thread by Astley Le Jasper | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.