Decimals to Fractions

is there a built in fraction function (similar to excel or even python) which will show a decimal as a fraction?

i have a gh definition of something i build often but dimensions vary nearly every time… so i want to have the various objects dimensioned as a quick means to provide a cut list to the crew… i’d rather not give them dimensions in decimal inches :wink:

Inch_Fraction.gh (10.2 KB)

^ that’s what i’ve come up with so far… it works but it’s only specific to typical feet/inch… it wouldn’t be able to show .28571 as 2/7

so, does anyone know of more general decimal->fraction converting algorithm?

(as i understand, once python is hooked up in GH for Mac, i’ll be able to easily use python’s fraction module)

Eric Lippert in fact just blogged about a fraction finding algorithm a few weeks back.
Part 1, Part 2, Part 3, Part 4.

There is nothing in Grasshopper already which does this, but it sounds like it wouldn’t take too long to copy paste Eric’s code into a C# component. Would that solve your problem?

i think so.
would it work on Mac?

You work in sevenths of an inch… ? :stuck_out_tongue_winking_eye:

haha… no
just using it for an example… it seems there should be a more elegant solution than i’ve come up with though which is what i’m wondering about… mine specifically targets 1/4, 1/8, 1/16, 1/32, 1/64, or 1/128 increments… if my decimal isn’t first rounded to one of those increments, i’ll get an error.

I am soooo glad I don’t have to work in inches anymore. At one time I had all the decimal equivalents up to 32nds in my head… As a modelmaker I worked mostly in decimal inches though, unless we were building something construction like in scale, then 1/16’s were usually more than accurate enough - something you can actually see on a tape measure.

All the space that those fraction equivalents used to take up in my head are now used for storing some of the more esoteric Rhino options… :stuck_out_tongue_closed_eyes:

–Mitch

I’m definitely not a proponent for the continued use of imperial in the u.s…

but I don’t really have much of a choice… the govt/suppliers needs to step in here and make a change :wink:

1 Like

We don’t have support for C# components on Mac… yet

oh. ok. thanks.

so @DavidRutten-- that wouldn’t be much help for me right now… maybe some other people though?

Since the number of different fractions you’re willing to consider is limited, perhaps just compute them all and find the nearest solution?

The attached file only considers fractions between zero and one, so you still need to extract the integer part of a number first.

dedoublifier.gh (9.4 KB)

Sorry, my bad. You need Find Similar Member, not Member Index. Member Index only works if there’s an exact match.

dedoublifier.gh (9.4 KB)

i think the member index would work for me if i continue using parts of my first attempt…

i’m ok with the first part of my definition… it’s what happens after this part where i’m running into errors and mile long if/then statements…

the first part is:

• with the Value List, i can set the tolerance (4, 8, 16, 32, 64 etc… though generally, it’s either going to be 16 or 32)
• the decimal is multiplied by the tolerance, then rounded, then the modulus gives me how many units of my tolerance value…
• the list at right shows the numerator… so it’s 15/16, 0/16, 14/16, 1/16, 8/16, & 13/16.

what i need it to say is 15/16 , (nothing), 7/8, 1/16, 1/2, 13/16 …or, i’m already able to generate the fraction from the decimal at a specific tolerance… what i was unclear about in the first post is that i’m actually looking for a way to see a fraction in it’s simplest form… 24/32 as 3/4 … 6/16 as 3/8… 5/16 remains 5/16 etc.

so going off your suggestion, i should probably generate a list of all the 16ths… or all the 32nds (or whatever my tolerance happens to be at the time)… then somehow match my value?

i’ll give it a shot a little later… i have to go dig out another car right now.
thanks for the help.


[edit] oh- how are you putting that little note above the groups? i like that.


[edit2] hmm… i suppose i just noticed a problem i’m going to run into if continuing along this path…

Item 1— 30.994 is 0/16, which is correct… however, the resulting value should be 31" but it’s going to give me 30" in this scenario… i’ll have to check for that somehow.

Hi guys, I’ll never need this but it triggered my procastination so I built this. It should do the trick.

This is what it outputs (inputs to the left):

99.2158 ~ 99 7 / 32 (deviation is -0.00295 )
35.124 ~ 35 1 / 8 (deviation is -0.000999999999998 )
0.0078129 ~ 0 1 / 128 (deviation is 3.99999999999e-07 )
21.016 ~ 21 1 / 64 (deviation is 0.000374999999998 )


#Decimal to Fraction calculation by Holo
#2016-01-25

import rhinoscriptsyntax as rs
Fraction=128

number=rs.GetReal("Type number to imperialize")

wholeNumber=int(round(number,0))
if wholeNumber>number : wholeNumber=wholeNumber-1

deviation=number-wholeNumber

value=int(round(Fraction*deviation))

#set up n to prevent infinite loop
n=1

while (value % 2 == 0):
    #number is even
    value=int(value/2)
    Fraction=int(Fraction/2)
    n=n+1
    if n==20: break

if Fraction==0:
    finalDeviation=0
else:
    finalDeviation=number-wholeNumber-(value/Fraction)

print number, " ",chr(126)," ", wholeNumber, " ", value,"/",Fraction, "    (deviation is ",finalDeviation,")"




1 Like

nice Jørgen!

thing is, with python, it’s simple:

import rhinoscriptsyntax as rs
import fractions

decimal = .2158
fraction = fractions.Fraction(decimal)

print fraction

i did this one a while back which rounds to nearest 1/16 (or 1/8 or 1/32 etc)

Dimensions help - python

the problem, for me at least, is i’m now wanting to do some dimensioning via grasshopper except i’m on mac and mac grasshopper doesn’t have python :wink:

1 Like

i almost have a solution ready in grasshopper…

fraction_03.gh (7.3 KB)

it’s just a list of all the available 64ths then i pull from that.

i just need to check for the rounding to 0 error that i noticed earlier and put the inches back in the result… so, i think i’ll be good to go after i get time to work on this a little more.


the biggest problem i had was making the list… i’ll post a couple of questions about that later… this snow is taking up a lot of my free time :wink: …gotta go.

1 Like

ok… i’m going with this… seems to work ok.

decimalTOfraction.gh (8.2 KB)

thanks for the tips

3 Likes