When I use Rhino.UI.Localization.FormatNumber I get unexpected results and am trying to figure out if it’s me or the API method.
Based on my debug prints below it would appear that the method correctly parses floats with a single decimal place and inserts the - symbol but fails to do so on numbers greater than single decimal.
Furthermore, when the result is less than 1’ it returns inches instead of 0'-inches number here" which is (I believe?) more standard in representing Architectural feet/inches in drawings.
@michaelvollrath - can you provide me some code I can run here (without having to type up sample? Since you’re calling ActiveDoc, please include whatever .3dm file I’ll need to repeat.
None of the numbers are giving me a 0’-6" result directly but the first number in this sample I’m sharing gives me 6" by its lonesome. I’m simply pointing out here that from what I have seen in most architecture drawings, people still call out 0 for both feet or inches so that there’s not confusion.
So it returns 6" but I would expect 0’-6" or 0’ - 6" to be returned for consistency with the rest of the results. Same with a 0 value that would usually be written 0’-0" in architectural feet and inches drawings.
Model Units are Feet
Hi @dale, thanks for looking into it! Please see the sample code you should be able to run in any new Rhino file with Model Units set to Feet.
#! python 3
import Rhino
import scriptcontext
def FormatDistance(distance):
"""Given a number in model units, format to display in feet / inches (fractional)"""
try:
# inches = Rhino.UnitSystem.Inches
model_units = scriptcontext.doc.ActiveDoc.ModelUnitSystem
feetInches = Rhino.UI.DistanceDisplayMode.FeetInches
precision = 4
formatted_number = Rhino.UI.Localization.FormatNumber(distance, model_units, feetInches, precision, False)
print(f"FormatDistance result: {formatted_number}")
return formatted_number
except Exception as ex:
Rhino.RhinoApp.WriteLine(f"FormatDistance Exception: {ex}")
if __name__ == "__main__":
formatted_number_result = []
test_numbers = [
0.49999999999999994,
13.999999999999996,
26.0,
37.99999999999999,
49.99999999999999,
35,
0,
1.0,
1.0000000000001]
for number in test_numbers:
formatted_number = FormatDistance(number)
formatted_number_result.append(formatted_number)
print(f"formatted_number_result: {formatted_number_result}")
Thanks @dale for clarifying. I actually am using it more broadly and specifically for displaying text in custom user interface elements depicting data such as “Above finished floor heights” for objects, elevation marker points, levels, etc… “non native” Rhino annotation objects if you will.
Many of these things are dynamically connected to custom UI elements where the text is displayed.
So the 0 suppression turned off is how I currently have annotations set up and as you pointed out, that works great but I need to also have an optional argument for zero suppression to be disabled in the FormatNumber function if possible.
I could write some custom logic for this but it felt more like a bug or unexpected result of the FormatNumber function.
So, if it’s not a bug then I guess I would like to formally request for that to be a feature of the FormatNumber function.