Convert strings to numbers

Numbers stored in User Text are strings that cannot be calculated as numbers unless they are converted to real numbers by the float() function.

For example:

To calculate the weight of an object from its volume and material density, you can:

  1. Use the Volume function to retrieve the volume of the object, like:

    %<Volume("ObjectID")>%

  2. Add a "Density" key to the object's Attribute User Text to define the material density that can be retrieved by the AttributeUserText function, like:

    %<UserText("ObjectID","Density")>%

  3. Multiply Volume() by UserText().

    %<Volume("ObjectID") * UserText("ObjectID", "Density")>%

    This formula will fail because UserText() returns a string, not a number. To make it work, use float() to convert UserText() from a string to a number, like:

    %<Volume("ObjectID") * float(UserText("ObjectID", "Density"))>%

 

 

 

Rhino 7 for Mac © 2010-2021 Robert McNeel & Associates. 13-juil.-2021