Ugo DI LUCA Posted November 14, 2003 Posted November 14, 2003 Hi, I've been asked on a PM how this could be done. I had a few ideas but not the exact one, and reported the poster to add his querry to the boards instead. As he didn't, and I'm still curious about how FM can handle this, here it is : How to convert .5 to 1/2, .33 to 1/3 , 3.8 to 3 4/5 ..... The PM was in fact : "I need to use a table where I have to capture lots of data but I need to capture it and present it after calculation in inches, (ex, 1/2, 3 1/7, not .5 ) do you have any idea for handling this? Thanks a lot "
Vaughan Posted November 14, 2003 Posted November 14, 2003 Avoid the issue altogether: the value is entered as a fraction in a text field, which is then parsed into decimal format. Sorry Ugo, I'm really struggling for ideas that don't involve pages of hard-coded Case functions.
BobWeaver Posted November 14, 2003 Posted November 14, 2003 The following script will do it: Set Field [ gNum , DecimalNumber ] Set Field [ p , DecimalNumber ] Set Field [ q , 1 ] Loop
BobWeaver Posted November 14, 2003 Posted November 14, 2003 To do it as a calculated field it's easiest to break it into sub-calculations. Otherwise it gets very ugly. This seems to work okay: DecNum is the decimal number you want to display as a fraction Precision=.00000001 p0=Mod(Abs(DecNum),1) p1=1/Case(Mod(p0,1)<Precision,1,Mod(p0,1)) p2=1/Case(Mod(p1,1)<Precision,1,Mod(p1,1)) p3=1/Case(Mod(p2,1)<Precision,1,Mod(p2,1)) p4=1/Case(Mod(p3,1)<Precision,1,Mod(p3,1)) p5=1/Case(Mod(p4,1)<Precision,1,Mod(p4,1)) Fraction= Case(Int(DecNum),Int(DecNum), Sign(DecNum)<0,"-","") & Case(p0," "&Round(p0 * p1 * p2 * p3 * p4 * p5,0) &"/"&Round(p1 * p2 * p3 * p4 * p5,0),"")
Ugo DI LUCA Posted November 14, 2003 Author Posted November 14, 2003 Nice one Bob, I played a little with a kind of weird calcs, but couldn't get passed the 2 decimals (0,666666 or 0,33333 showing as 2/3 or 1/3. I'll have a try with yours. BTW, is there any existing rules for fractions ? .40 could show either as 4/10 or 2/5 .005 could show either as 5/100 or 20/500...(may be others...)
BobWeaver Posted November 15, 2003 Posted November 15, 2003 The method I posted is known as a finite continued fraction product. It is exact within the limits of Filemaker's numeric precision, and will produce the fraction with the smallest denominator. So, .40 will produce a result of 2/5, and 4.142857143 will give a result of 4 1/7. However you have to be careful when the input value is not exact. For example a value of .333 will not produce a result of 1/3, because it's not 1/3, it is 333/1000. But an input value of .333333333 will produce a result of 1/3. So, you have to play around with the precision value to make sure that it doesn't give you strange results. After I posted the method above, I cleaned it up a bit to avoid a couple of glitches. Here are the revised ones: Precision = .00001 p0 = Abs(DecNum) p1 = 1/Mod(p0,1) p2 = 1/Case(Mod(p1,1)<Precision,1,Mod(p1,1)) p3 = 1/Case(Mod(p2,1)<Precision,1,Mod(p2,1)) p4 = 1/Case(Mod(p3,1)<Precision,1,Mod(p3,1)) p5 = 1/Case(Mod(p4,1)<Precision,1,Mod(p4,1)) p = Round(p0*p1 * p2 * p3 * p4 * p5,0) q = Round(p1 * p2 * p3 * p4 * p5,0) cFraction = Case(Sign(DecNum)<0,"-","")& Case(Int(p/q),Int(p/q),"") & Case(Mod(p,q), " "&Mod(p,q) &"/"&q,"") I noticed that the original message had asked for fractions like 1/2, 3 1/7, etc. I don't know if that was a typo, but I can't imagine that anyone would want a measurement in sevenths of an inch. However, that is exactly what you will get from the continued fraction method. It would probably be better to give the result in fractions where the denominator is a power of 2. The following will give the result to the nearest 64th: cNumerator64 = Abs(Round(DecNum*64,0)) cFactor64 = 2^( (Mod(cNumerator64/64, 1)=0)+ (Mod(cNumerator64/32, 1)=0)+ (Mod(cNumerator64/16, 1)=0)+ (Mod(cNumerator64/8, 1)=0)+ (Mod(cNumerator64/4, 1)=0)+ (Mod(cNumerator64/2, 1)=0)) cFraction64 = Choose(Sign(Round(DecNum * 64,0))+1,"-","0","")& Case(Int(cNumerator64/64),Int(cNumerator64/64),"") & Case(Mod(cNumerator64,64), " "&Mod(cNumerator64/cFactor64,64/cFactor64) &"/"&(64/cFactor64),"")
Ugo DI LUCA Posted January 2, 2004 Author Posted January 2, 2004 Wow... Thanks Bob. I even forgot about this post, with all the Forums strange behaviour. I hope who originally asked me found it useful.
BobWeaver Posted January 3, 2004 Posted January 3, 2004 I had learned how to do this a few years ago, but had completely forgotten the method. So, it was interesting to go back and dig up the info again.
Lee Smith Posted January 3, 2004 Posted January 3, 2004 Hi Ugo, I was going to send you a file that I made up about a year ago using Bob's step-by-step instructions, howeverr he beat me to the punch when he posted it here again. Too much going on and not enough time. Hi Bob, I was also trying to post a link to your previous post, so I did a search of the archives, but I'll be darned if I could find it. I was wondering if you could have posted it to a different List? Lee
BobWeaver Posted January 4, 2004 Posted January 4, 2004 Lee, other than this one, I don't remember any other post that I did on this topic. Maybe it was someone else?
Recommended Posts
This topic is 7699 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now