Page 1 of 1

Convert Decimal to Fraction?

Posted: Mon Oct 12, 2020 8:11 pm
by cmhjon
Hi all,

I need to be able to convert a decimal value to its fraction equivalent (for display purposes in a text field). Suppose I have "4.28125" in a text field. Via script, I need to convert it so that "4 9/32" shows in the text field.

Is there a way to do this?

Thank you,
Jon

Re: Convert Decimal to Fraction?

Posted: Mon Oct 12, 2020 9:40 pm
by kdjanz
Nope! The world has gone metric and fractions are outlawed! Sorry.
😀

Re: Convert Decimal to Fraction?

Posted: Mon Oct 12, 2020 11:55 pm
by bn
Hi Jon,

something along these lines?

fraction.livecode.zip
(1.4 KiB) Downloaded 263 times

Kind regards
Bernd

Re: Convert Decimal to Fraction?

Posted: Tue Oct 13, 2020 1:44 am
by dunbarx
How accurate do you need those fractions? Bernd's stack works fine given that the decimal portion is rounded to the nearest power of 2 in the denominator. If this is OK, then you need go no further.

For example, an argument of 1.144 with a 32 in Bernd's stack gives 4/32, simplified to 1/8. This is 1.125, somewhat close. Of course, if you raise the denominator to 128, you get, after simplifying, 9/64, or 1.40625. Even closer.

To get closer yet, one needs to allow any denominator of reasonable length. I might play around with this just because I love LC. But how accurate do you need your results?

Craig

Re: Convert Decimal to Fraction?

Posted: Tue Oct 13, 2020 3:31 am
by dunbarx
This was much easier than I thought, and I bet the method has been around since Agincourt. It gives exact fractions for any decimal number.

On a new card make two fields and a button. Put any decimal number into fld 1. In the button script:

Code: Select all

on mouseup
   get fld 1
   put trunc(it) into wholes
   if wholes = 0 then put "" into wholes
   set the itemDel to "."
   put item 2 of it into numer
   put 10 ^ (the length of numer) into denom
   put makeFrac(wholes,numer,denom) into fld 2
end mouseup

function makeFrac wholes,numer,denom
   put 2 into index
   put numer into maxLoops
   repeat until index > maxLoops
      if numer mod index = 0 and denom mod index = 0 then
         divide numer by index
         divide denom by index
         next repeat
      else
         add 1 to index
         next repeat
      end if
   end repeat
   return wholes && numer & "/" & denom
end makeFrac
Craig

Re: Convert Decimal to Fraction?

Posted: Tue Oct 13, 2020 7:17 pm
by cmhjon
Hi dunbarx,

I tested your script and it works as expected, thank you so much! After posting this thread, I thought about adding a text field containing a list with each line containing a decimal value and its fraction equivalent separated by a comma and doing a lookup but I like this better.

Best regards,
Jon

Re: Convert Decimal to Fraction?

Posted: Tue Oct 13, 2020 8:01 pm
by dunbarx
You are welcome.

Not sure how far you take your decimal values, but it sounds like that might be a very long list.

When I was fooling around with this, I wondered if a decimal like 2.967, which has to be rendered as 2 967/1000 was just too cumbersome, and whether or not in such cases a close, but smaller fraction would do. Would it?

Craig

Re: Convert Decimal to Fraction?

Posted: Tue Oct 13, 2020 8:15 pm
by cmhjon
Hi Craig,

My intention is that if the decimal is an unusual number like your example, the decimal will still be displayed so I may still need add that text field such that if the digits after the decimal point don't appear in the text field, the conversion handler is bypassed and the decimal displayed.

For the purposes of my app, it will display fractions down to 32nd's as I doubt it will ever get down to 64th's or smaller but who knows.

Best regards,
Jon :)

Re: Convert Decimal to Fraction?

Posted: Tue Oct 13, 2020 11:46 pm
by dunbarx
Ah,

So all your decimals are exact multiples of 0.03125 (1/32)?

Craig

Re: Convert Decimal to Fraction?

Posted: Wed Oct 14, 2020 12:37 am
by cmhjon
There...could...be some cases where the decimal is not a multiple of 1/32nd. When that happens, the decimal value will be displayed.

Best regards,
Jon :)

Re: Convert Decimal to Fraction?

Posted: Wed Oct 14, 2020 4:37 am
by dunbarx
...not a multiple of 1/32nd. When that happens, the decimal value will be displayed.
It would be simple to test the resulting fraction, and if, say, the length of both the numerator and denominator were equal to or greater than three, then display the decimal.

But there are decimals that reduce to "simple" fractions. For example, 2,440 reduces to 2 11/25. Not in the powers-of-two denominator world, but a fraction that ought not scare anybody.

Craig