Converting Decimal Inches to Decimal Fractions without the Units

How do I convert a decimal from decimal inches to factional inches? I’ve tried the following code using python. LengthString does give me the length in fractional inches but I do not want the units. Is there a way using LengthValue to get only the numeric (fractional) part of the length without the units? Is there another class that I could use in Python to get this? How does the distance command work in Rhino where you can change the units?

    dec = 5.875
    tempLV = Rhino.LengthValue.Create(dec,  us.Inches, Rhino.LengthValue.StringFormat.CleanProperFraction)
    print tempLV.LengthString

Hi MIke - LengthValue is looking for a unit system, so you need to set that up first, or spell it out explicitly as you have done with the next variable - Rhino.LengthValue.StringFormat.CleanProperFraction

import Rhino
dec = 5.875
usInches = Rhino.UnitSystem.Inches
tempLV = Rhino.LengthValue.Create(dec,  usInches, Rhino.LengthValue.StringFormat.CleanProperFraction)
print tempLV.LengthString

Does that do what you need?
-Pascal

Hey Pacal. Thanks for looking at this. I forgot to include my include statement:

import Rhino.LengthValue as lv

So I was referencing Rhino.UnitSystem. Is there anyway to get the result WITHOUT the units at the end of the string. I’m looking for only the numeric part including the fraction. I have a bunch of calls to convert the unit and will parse the numeric part out if I had to but I wanted to make sure there wasn’t an easier way. Also is this what Rhino’s distance command uses?

Also… I know how to get the document’s unit settings display precision via sc.doc.ModelDistanceDisplayPrecision. If the display precision is set to 1’1-1/8" and I use the distance command to measure a line that is 5.88", the result is 5-7/8", the closest 1/8th of an inch so it’s following the display precision. Is there a way I can do the same with LengthValue or another class? If I try to use CleanProperFraction as the StringFormat, LengthValue returns 5.88 inches rather than the closest 1/8th of an inch (5-7/8). How does the distance command work since it returns the closest fraction according to the displace precision?

I noticed this as well. I’ll see what I can find out - Dale is away at the moment though, he’s my usual source.

-Pascal

Does this help?

import Rhino

inches = Rhino.UnitSystem.Inches
decimal = Rhino.UI.DistanceDisplayMode.Decimal
feetInches = Rhino.UI.DistanceDisplayMode.FeetInches
precision = 3

value = 5.875

str0 = Rhino.UI.Localization.FormatNumber(value, inches, decimal, 3, False)
print(str0)

str1 = Rhino.UI.Localization.FormatNumber(value, inches, feetInches, 3, False)
print(str1)

– Dale

1 Like

Ya that worked! Thanks! Is that what the distance command in Rhino does?