Convert fraction string to decimal

I am getting the size of “MEP Fabrication Hangers” and I need this as a number to then do math and whatever else to it.

However I can’t seem to be able to convert it.

I thought there was a custom node that could handle this but I couldn’t find it.

You’ll have to break down the string into its feet, inches, and fractional parts to convert it back to a number. It won’t read that format of string. Python is definitely the best way to handle this, but it’s possible with nodes and a bit of effort.

You could also check and see if that parameter also stores the value numerically. If it does, that’s way easier.

EDIT: Don’t know how I missed it the first time… Springs has Fraction.ToFeet that does this, but it doesn’t seem to handle feet with fractional inches. If you need that functionality, you can probably add it yourself with a little effort.

That is a good node to remember, but yeah mine are all inches only. I have a limited number of hanger sizes so I just went manual on it.

Interesting. Not sure why it didn’t work for you. Might be something in an updated version.
image

There’s probably extra syntax to handle in imperial, but for basic whole/fraction conversion this could work:

myNumbers = ["1 1/4\"", "1 1/2\"", "2 1/2\"", "3 1/2\"", "1/2\"", "3/4\"", "1\"", "10\""]

def fractionToNumber(string):
    # Split at the space
    string = string.replace("\"", "")
    splitStrings = string.split(" ")
    # If we have one string...
    if len(splitStrings) == 1:
        # If it is a fraction
        if "/" in string:
            product, quotient = string.split("/")
            return int(product)/int(quotient)
        # Else it is a number
        else:
            return int(string)
    # Otherwise we have two strings...
    else:
        # Assign the parts
        whole, fraction = splitStrings
        # Handle the fraction
        product, quotient = fraction.split("/")
        # Return the total
        return int(whole) + int(product)/int(quotient)

for n in myNumbers:
    print(fractionToNumber(n))
1 Like