Convert Feet and Inches string to Real

When entering a value for a length in Rhino, you can input a decimal number like 1.5 or you can input a fractional feet-and-inches string like 10’-4 1/4, and Rhino will automatically convert this string to a decimal value. Is there a way to access this conversion method via python? I’m getting a list of lengths from the user that I need to convert to real floats, and I want to allow the user to input the lengths as fractional feet-and-inches strings just like they could for any other Rhino command.

Hmm how are you getting the inputs? The command-line input is pretty strict, I presume people just typing in numbers in a form are going to add extra spaces anywhere, so it’s not really likely to work, you’ll have to roll your own conversion…there’s probably a python library somewhere for that?

It’s not too much trouble to write my own. I’ll just do that.

As a starting point, take a look at the RhinoCommon LengthValue class LengthValue Class (rhino3d.com) and the LengthValue.StringFormat Enumeration LengthValue.StringFormat Enumeration (rhino3d.com).

Regards
Jeremy

Hi @Measure,

Use RhinoMath.TryParseNumber:

import Rhino

text = "10'-4 1/4"
rc, value = Rhino.RhinoMath.TryParseNumber(text)
if rc:
    print(value)

– Dale

6 Likes

RhinoMath.TryParseNumber Method
Evaluates command line math expression.

wow that s nice.

This is exactly what I needed. Thanks!