decimal fraction conversion
-
Hello All, I need to be able to convert decimal numbers into fraction form. I understand how to do this mathematically but I'm not sure how to put it into code. Thanks for your help Aaron :beer
-
Hello All, I need to be able to convert decimal numbers into fraction form. I understand how to do this mathematically but I'm not sure how to put it into code. Thanks for your help Aaron :beer
I know how you feel. Before I took a programming class, I didn't have a clue how to translate a mathematical algorithm into code. Taking that C class in college really helped. Perhaps you should try that yourself. If you have taken a class and/or read a few books, then you should at least attempt to convert your algorithm into code. Once you do, compile your code and see if it works. If you get stuck, come back here and show us your work so we can help you pinpoint the problem. Good luck! Regards, Alvaro
That which does not kill me postpones the inevitable. -- despair.com
-
Hello All, I need to be able to convert decimal numbers into fraction form. I understand how to do this mathematically but I'm not sure how to put it into code. Thanks for your help Aaron :beer
Hey Dude, A quick 'n' dirty way of doing it would be to count the number of digits after the decimal point as your numerator and use this number of digits to calculate your denominator. Ex: .3456 = 3456/10000 That'd give you a fraction at least. If you want to reduce it, find numbers that divide both the numerator and denominator evenly. Use the modulus operator for this. A result of 0 indicates that the number is evenly divisible. ex: 2%10=0, evenly divisible 3%10=1, not evenly divisible. Of course for very long decimals you may want to adopt some fixed amount of places to round off to. You will lose some precision in the number however. I'd put the whole operation in a function like: void print_fraction(double decimal_value), that prints a fraction for the decimal value you pass to it. Hope this helps. Josh Knox