Convert Decimal to Fraction?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
cmhjon
Posts: 75
Joined: Tue Aug 04, 2015 11:55 am

Convert Decimal to Fraction?

Post by cmhjon » Mon Oct 12, 2020 8:11 pm

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

kdjanz
Posts: 232
Joined: Fri Dec 09, 2011 12:12 pm
Location: Fort Saskatchewan, AB Canada

Re: Convert Decimal to Fraction?

Post by kdjanz » Mon Oct 12, 2020 9:40 pm

Nope! The world has gone metric and fractions are outlawed! Sorry.
😀

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3447
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Convert Decimal to Fraction?

Post by bn » Mon Oct 12, 2020 11:55 pm

Hi Jon,

something along these lines?

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

Kind regards
Bernd

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7168
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Convert Decimal to Fraction?

Post by dunbarx » Tue Oct 13, 2020 1:44 am

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7168
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Convert Decimal to Fraction?

Post by dunbarx » Tue Oct 13, 2020 3:31 am

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

cmhjon
Posts: 75
Joined: Tue Aug 04, 2015 11:55 am

Re: Convert Decimal to Fraction?

Post by cmhjon » Tue Oct 13, 2020 7:17 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7168
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Convert Decimal to Fraction?

Post by dunbarx » Tue Oct 13, 2020 8:01 pm

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
Last edited by dunbarx on Tue Oct 13, 2020 11:44 pm, edited 1 time in total.

cmhjon
Posts: 75
Joined: Tue Aug 04, 2015 11:55 am

Re: Convert Decimal to Fraction?

Post by cmhjon » Tue Oct 13, 2020 8:15 pm

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 :)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7168
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Convert Decimal to Fraction?

Post by dunbarx » Tue Oct 13, 2020 11:46 pm

Ah,

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

Craig

cmhjon
Posts: 75
Joined: Tue Aug 04, 2015 11:55 am

Re: Convert Decimal to Fraction?

Post by cmhjon » Wed Oct 14, 2020 12:37 am

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 :)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7168
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Convert Decimal to Fraction?

Post by dunbarx » Wed Oct 14, 2020 4:37 am

...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

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”