Using round() in a SketchUp plugin
-
Hi, I'm new here (and pretty new to Ruby, as well as plugin creation) so if this is in a horribly wrong place let me know and I'll re-post.
In any case, I've got the following code in my plugin, and it's just not working.
percent = 100*array[0] percent = percent.round(2)
I believe it should work, as this is the syntax I saw here, but SketchUp doesn't seem to recognize it. I'm just looking to take an unwieldy float value out of an array and tidy it up to two decimal places before putting it into a messagebox.
Thanks!
-
You haven't provided the whole picture.
Let's assume that 'array
' is an array of numbers.
You need to pass a 'float' to '.round' so try using100.0
instead of the 'integer'100
- that will then force the answer into a float even whenarray[0]
is an integer... -
Hi Steve,
The Ruby version (1.8.6) which comes with SketchUp does not support the argument to round. This came in a later Ruby version. 1.8.7 I think.
Anyway, you can access the 1.8.6 docs from the same site: http://www.ruby-doc.org/core-1.8.6/index.html
-
To add a round 'range' etc to the current version of SUp's Ruby copy/paste this into a
float_extras.rb
file in Plugins to add these additional methodsclass Float def round_to(x) (self*10**x).round.to_f/10**x end def ceil_to(x) (self*10**x).ceil.to_f/10**x end def floor_to(x) (self*10**x).floor.to_f/10**x end end =begin Example usage; num = 138.249 num.round_to(2) # => 138.25 num.floor_to(2) # => 138.24 num.round_to(-1) # => 140.0 =end
replace
.round
with.round_to(n)
etc... -
@jim said:
Hi Steve,
The Ruby version (1.8.6) which comes with SketchUp does not support the argument to round. This came in a later Ruby version. 1.8.7 I think.
Anyway, you can access the 1.8.6 docs from the same site: http://www.ruby-doc.org/core-1.8.6/index.html
That's a shame. In any case, I came up with a workaround, I suppose it's pretty obvious but I thought it might help anyone else who needs to do some rounding in SU:
percent = (10000*array[0]).to_i percent = percent.to_f/100
Just add equal numbers of zeroes to the numbers if you want greater precision. As it's written, it rounds to two decimal places.
-
There is also a backport ruby gem.. that "updates" older Ruby versions with features from newer releases.
But the weird thing is that then
RUBY_VERSION
andRUBY_PATCHLEVEL
become meaningless. -
TIG's example WILL work, but requires that EVRYONE have that "extension".
You might put those methods inside a library module, inside your toplevel "author" module.
-
@dan rathbun said:
TIG's example WILL work, but requires that EVRYONE have that "extension".
You might put those methods inside a library module, inside your toplevel "author" module.
Well, does my simplified attempt at rounding above work? I only need to use it one time, so I would think it'd be faster just to do it via my method.
-
@steve r said:
Well, does my simplified attempt at rounding above work?
No the
round()
function actually rounds.. theto_i()
function actually truncates.Use the "meat" of TIG's example in a single statement
.. or write methods into your module thus:module Author; module ThisPlugin; end; end module Author;;ThisPlugin class << self # proxy class private # just to show that percent() will be private def percent( num ) (num*10**-2) end public # just to show how to return to the default # of making following methods public. def round_number( num, places=0 ) (num*10**places).round.to_f/10**places end end # proxy class begin num = 78.67812 # just for example per = percent( round_number(num,2) ) end end # module
-
@dan rathbun said:
No the
round()
function actually rounds.. theto_i()
function actually truncates.Use the "meat" of TIG's example in a single statement
.. or write methods into your module thus...Good point! I hadn't considered that my method just truncates. I'll have to see if I can figure out a simple way to incorporate TIG's example into one statement; a lot of that code goes right over my head! Thanks for the help!
Advertisement