Best way to cast length to string

Hi

This is really bugging me.

I want to create a point using:
pt = Geom::Point3d.new(1.m,1.m,1.m)

and then write the value to a string WITHOUT the metres “m”
pt.x.to_m.to_s

Is this really the neatest way to do this?

First of all, (for general info,) there are some nifty module methods
for creating numeric strings that use the user’s current model units.


No, your way means you must interrogate the model options to determine what the user’s model units are and adapt (as in a complex branching statement with multiple conditional expressions.)

This way you do not:

pt.x.to_s.to_f.to_s

The first to_s is the API’s Length class method that returns a numeric string in the user’s current model units WITH the unit symbol. The to_f then converts back to a number without regard to model units (so basically just removes any unit symbol,) and the last to_s is the Ruby core method for the Float class.

What I’d do is wrap it up in a custom method and run the result through the global format method to control how many decimal places and leading spaces the string has.