Rounding

Rounding

Postby morci429 » Tue Feb 01, 2011 4:26 pm

Hi everyone,

As you all know i'm knew to Sketchup ruby and i can't find this anywhere online.
I have a variable that has a float number and i wanted to round it.

Var = 1.8854523497846
Desired answer Var = 1.9

Also if i can recommend any online sources for Sketchup ruby I’ll be thankful

Thanks
0

morci429 
 

Re: Rounding

Postby Didier Bur » Tue Feb 01, 2011 4:56 pm

Hi,

Var = 1.8854523497846

Caution: this will create a constant (not very suitable for a variable)

Code: Select all
var = 1.8854523497846

is better ;)

Code: Select all
a.floor # returns 1
a.ceil #returns 2
a.round #returns 2
a.round(1) #should return 1.9, but appears not to work as expected

SU uses Ruby 1.8.6 and .round(x) is new in Ruby 1.9. You'll have to wait for SU9 or code it yourself, or like this:
Code: Select all
def prec(x)
      a=self.to_s.split(".")
      if not a[1]
         return self
      end
      if a[1].length > x-1
         return(a[0] + "." + a[1][0..(x-1)]).to_f
      else
         dif=x-a[1].length
         return(a[0] + "." + a[1] + ("0"*dif)).to_f
      end
   end


Code: Select all
var.prec(6) #returns 1.885452
:bounce:
See http://www.ruby-doc.org/core/ and select the 'Float' class.

For online SketchUp-Ruby docs, see: http://forums.sketchucation.com/viewtopic.php?f=180&t=10142 (well done Dan !)
Hope this helps,
0
Didier Bur
Ecole Nationale Supérieure d'Architecture de Nancy (F)
User avatar
Didier Bur 
 

Re: Rounding

Postby Dan Rathbun » Tue Feb 01, 2011 5:24 pm

@morci429: In reality.. there is no such think as "Sketchup Ruby." There is only Ruby, that that gets loaded by the Sketchup application, and then loads special Google API modules and classes, that extend Ruby so we can work with SKP models.

You need to learn Standard Ruby.. first. And you must have the Reference for the Standard Ruby Modules and Classes.

Didier Bur wrote:I suppose SU uses an older version of standard Ruby ?
See http://www.ruby-doc.org/core/ and select the 'Float' class.

Yes, you poined to the current Ruby 1.9.x core.

I have links to the online 1.8.6 and 1.8.7 Core Refs, as well as downloadable offline CHM Refs in this post:
RUBY PROGRAMMING REFERENCES
0
    I'm not here much anymore. But a PM will fire email notifications.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    Re: Rounding

    Postby Dan Rathbun » Tue Feb 01, 2011 5:50 pm

    How about:
    Code: Select all
    var = 1.8854523497846
    var1 = ((var*10.0).round)/10.0


    OR for a singleton Mixin method, save as 'mixin/float_round_to.rb'
    (make a subfolder under plugins called 'mixin' and put the file there.):
    Code: Select all
    module Mixin; end

    module Mixin::Round_to
      def round_to(num)
        places = 10.0**num
        return ((self.to_f*places).round)/places
      end
    end #module

    then use it by extending your variable:
    require('mixin/float_round_to.rb')
    var.extend(Mixin::Round_to)
    var1 = var.round_to(1)

    >> 1.9
    var3 = var.round_to(3)
    >> 1.885
    var8=var.round_to(8)
    >> 1.88545235
    0
      I'm not here much anymore. But a PM will fire email notifications.
      User avatar
      Dan Rathbun 
      PluginStore Author
      PluginStore Author
       

      Re: Rounding

      Postby morci429 » Wed Feb 02, 2011 1:45 pm

      Many thank everyone

      Dan Rathbun you are a genies
      0

      morci429 
       

      Re: Rounding

      Postby TIG » Wed Feb 02, 2011 2:12 pm

      If you want to round it as output [as astring] then you can use
      var_string = sprintf("%.1f", var)
      for 1 dp etc
      0
      TIG
      User avatar
      TIG 
      Global Moderator
       

      SketchUcation One-Liner Adverts

      by Ad Machine » 5 minutes ago



      Ad Machine 
      Robot
       



       

      Return to Developers' Forum

      Who is online

      Users browsing this forum: Jorgen10 and 7 guests

      Visit our sponsors: