sketchucation logo sketchucation
    • Login
    πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Rounding

    Scheduled Pinned Locked Moved Developers' Forum
    6 Posts 4 Posters 2.0k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M Offline
      morci429
      last edited by

      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

      1 Reply Last reply Reply Quote 0
      • Didier BurD Offline
        Didier Bur
        last edited by

        Hi,

        @unknownuser said:

        Var = 1.8854523497846

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

        var = 1.8854523497846
        

        is better πŸ˜‰

        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:

        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
        
        var.prec(6) #returns 1.885452
        

        πŸŽ‰
        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,

        DB

        1 Reply Last reply Reply Quote 0
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by

          @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 said:

          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:
          [url=http://forums.sketchucation.com/viewtopic.php?f=180&t=10142#p266725:1bdc3prs]RUBY PROGRAMMING REFERENCES[/url:1bdc3prs]


          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            How about:

            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.):

            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

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • M Offline
              morci429
              last edited by

              Many thank everyone

              Dan Rathbun you are a genies

              1 Reply Last reply Reply Quote 0
              • TIGT Offline
                TIG Moderator
                last edited by

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

                TIG

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                Buy SketchPlus
                Buy SUbD
                Buy WrapR
                Buy eBook
                Buy Modelur
                Buy Vertex Tools
                Buy SketchCuisine
                Buy FormFonts

                Advertisement