sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Using round() in a SketchUp plugin

    Scheduled Pinned Locked Moved Developers' Forum
    10 Posts 4 Posters 415 Views 4 Watching
    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.
    • S Offline
      steve r
      last edited by

      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!

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

        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 using 100.0 instead of the 'integer' 100 - that will then force the answer into a float even when array[0] is an integer...

        TIG

        1 Reply Last reply Reply Quote 0
        • J Offline
          Jim
          last edited by

          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

          Hi

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

            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 methods

            class 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...

            TIG

            1 Reply Last reply Reply Quote 0
            • S Offline
              steve r
              last edited by

              @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.

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

                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 and RUBY_PATCHLEVEL become meaningless.

                I'm not here much anymore.

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

                  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.

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • S Offline
                    steve r
                    last edited by

                    @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.

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

                      @steve r said:

                      Well, does my simplified attempt at rounding above work?

                      No the round() function actually rounds.. the to_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
                      

                      I'm not here much anymore.

                      1 Reply Last reply Reply Quote 0
                      • S Offline
                        steve r
                        last edited by

                        @dan rathbun said:

                        No the round() function actually rounds.. the to_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!

                        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