By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,678 Members | 1,129 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,678 IT Pros & Developers. It's quick & easy.

convert fractions into decimal

P: 59
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
Share this Question
Share on Google+
5 Replies


TheServant
Expert 100+
P: 1,168
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
Expert 100+
P: 1,168
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
Expert 100+
P: 1,584
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
Expert 100+
P: 1,168
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

P: 59
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.