[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: display decimal as normal fraction
From:       Michael Pyne <pynm0001 () comcast ! net>
Date:       2004-05-03 19:48:31
Message-ID: 200405031548.32404.pynm0001 () comcast ! net
[Download RAW message or body]

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Monday 03 May 2004 12:31, M2George wrote:
> Is there any way that I could take a value from a variable and then display
> that variable as a normal fraction.

Yes.  Have your program take the fraction part of the number (e.g., 0.5), and 
count the number of significant digits (1 in this example).  Form a fraction 
with the fraction part of the number over 1 followed by the number of 
significant digits (5/10).  You can then simplify by dividing both top and 
bottom by the gcd.  (Google for Euclid gcd algorithm if you need help with 
that).

Then, add the integer part of the number multiplied by the denominator to the 
numerator.

So, 1.5 would become:
0.5
5 / 10
1 / 2
3 / 2

There is a method of converting infinitely repeating decimal digits (like 
1.212121...) to an improper fraction as well.

> ie: 3.142857143 as 22/7 only on top of each other.

This will never happen though, unless your program can detect somehow that the 
142857 part is repeated.

You know, it would probably be much easier to simply keep the math done using 
improper fractions the whole time instead of converting at the end.

Regards,
 - Michael Pyne
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQFAlqIPqjQYp5Omm0oRAu/zAJ94yIM76NPOHEKPl5yfFngVNuKQcwCdHiUT
ebcG7Ud3LFSs0GlRqn+0PPs=
=CJZD
-----END PGP SIGNATURE-----
 
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic