Rhino.UI.Localization.FormatNumber - Unexpected Results

Hello,

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.

Input:
elevation_from_data: 0
scaled_elevation: 0.0
FormattDistance result: 0"
formatted_elevation: 0"

elevation_from_data: 0.15239999999999998
scaled_elevation: 0.49999999999999994
FormattDistance result: 6"
formatted_elevation: 6"

elevation_from_data: 4.267199999999999
scaled_elevation: 13.999999999999996
FormattDistance result: 14’0"
formatted_elevation: 14’0"

elevation_from_data: 7.924799999999999
scaled_elevation: 26.0
FormattDistance result: 26’-0"
formatted_elevation: 26’-0"

elevation_from_data: 11.582399999999998
scaled_elevation: 37.99999999999999
FormattDistance result: 38’0"
formatted_elevation: 38’0"

elevation_from_data: 15.239999999999998
scaled_elevation: 49.99999999999999
FormattDistance result: 50’0"
formatted_elevation: 50’0"

Expectation:
38’-0"
26’-0"

Result: :cross_mark: :white_check_mark:
image

Expectation:
38’-0"
0’-6"

Result: :cross_mark: :cross_mark:

Code:

    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"FormattDistance result: {formatted_number}")
            return formatted_number

        except Exception as ex:
            Rhino.RhinoApp.WriteLine(f"Utils.FormatDistance Exception: {ex}")

Thanks for your help!

Feels like a bug to me! Nice find. I’ll create a fix. I really appreciate all the code and examples to make reproducing easy as pie @michaelvollrath!

Ticket:

Questions

  • Which number is giving you the below, I can’t get a 0'-6" result

Expectation:
38’-0"
0’-6"

  • Are your model units Meters?
1 Like

@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.

– Dale

1 Like

Thanks for checking it out @CallumSykes!

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}")

Thank you both!

1 Like

Hi @michaelvollrath,

Rhino.UI.Localization.FormatNumber is not used when formatting annotations. It’s used strictly in user-interface elements.

Annotations can format 0.5 in the manner you want:

Just set the dimension style’s “Zero suppression” property to “No zero suppression.”

– Dale

1 Like

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.

Thank you!