Rounding numbers
-
I have a number I want to round to 6 decimal places before changing it to a string for output. The code I have in hand is (self*1000000.0).to_i.to_f/1000000.0 which appears to simply truncate instead of doing a true rounding.
As part of the float class, round(n) is a method in the Ruby language but I see no reference to it the Sketchup API. Is there a way to get around the SU API to get to the language methods?
Suggestions?
-
You can use round(n) or whatever the syntax is. It comes as part of the standard ruby 1.8.6 that comes with SU. The SketchUp API only documents methods and classes and modules that SU adds to the standard ruby. I'm sure I got some of the fine details of that wrong, but that is the general idea (awaiting a more complete answer from a more knowledgeable source).
-
@chris fullmer said:
You can use round(n) or whatever the syntax is. It comes as part of the standard ruby 1.8.6 that comes with SU.
Nope.
Float#round(n)
didn't show up until sometime after 1.8.6.@genma saotome said:
(self*1000000.0).to_i.to_f/1000000.0
If you are using "self", then you are doing it wrong. It's bad style to add or modify built-in methods in SketchUp's shared Ruby environment.
For the rounding, you never actually round - the docs say
to_i
truncates to an Integer.(self*1000000.0).round().to_f/1000000.0
-
Wow seriously? I was convinced I use a rounding method...I'm clearly losing it!
-
@chris fullmer said:
Wow seriously? I was convinced I use a rounding method...I'm clearly losing it!
http://ruby-doc.org/core-1.8.6/Numeric.html#method-i-round
It´s one of them methods tha´ts commonly modified.
-
Seems like updating the version of ruby used for Sketchup would he most helpful. Any reason for Trimble to do that as a routine step or is this one of those don't fix it if it ain't broke situations?
-
@genma saotome said:
Seems like updating the version of ruby used for Sketchup would he most helpful. Any reason for Trimble to do that as a routine step or is this one of those don't fix it if it ain't broke situations?
They've tried earlier, but it was troublesome. However, from the talks at Basecamp it did appear they are again looking to update the Ruby core.
Advertisement