Convert Decimal to Fraction?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Convert Decimal to Fraction?
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
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?
Nope! The world has gone metric and fractions are outlawed! Sorry.
-
- VIP Livecode Opensource Backer
- Posts: 4002
- Joined: Sun Jan 07, 2007 9:12 pm
- Location: Bochum, Germany
Re: Convert Decimal to Fraction?
Hi Jon,
something along these lines?
Kind regards
Bernd
something along these lines?
Kind regards
Bernd
-
- VIP Livecode Opensource Backer
- Posts: 9664
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Convert Decimal to Fraction?
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
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
-
- VIP Livecode Opensource Backer
- Posts: 9664
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Convert Decimal to Fraction?
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:
Craig
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
Re: Convert Decimal to Fraction?
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
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
-
- VIP Livecode Opensource Backer
- Posts: 9664
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Convert Decimal to Fraction?
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
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.
Re: Convert Decimal to Fraction?
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
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
-
- VIP Livecode Opensource Backer
- Posts: 9664
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Convert Decimal to Fraction?
Ah,
So all your decimals are exact multiples of 0.03125 (1/32)?
Craig
So all your decimals are exact multiples of 0.03125 (1/32)?
Craig
Re: Convert Decimal to Fraction?
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
Best regards,
Jon
-
- VIP Livecode Opensource Backer
- Posts: 9664
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Convert Decimal to Fraction?
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....not a multiple of 1/32nd. When that happens, the decimal value will be displayed.
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