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

    Rounding numbers

    Scheduled Pinned Locked Moved Developers' Forum
    7 Posts 4 Posters 376 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.
    • genma saotomeG Offline
      genma saotome
      last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • Chris FullmerC Offline
        Chris Fullmer
        last edited by

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

        Lately you've been tan, suspicious for the winter.
        All my Plugins I've written

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

          @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

          Hi

          1 Reply Last reply Reply Quote 0
          • Chris FullmerC Offline
            Chris Fullmer
            last edited by

            Wow seriously? I was convinced I use a rounding method...I'm clearly losing it!

            Lately you've been tan, suspicious for the winter.
            All my Plugins I've written

            1 Reply Last reply Reply Quote 0
            • thomthomT Offline
              thomthom
              last edited by

              @chris fullmer said:

              Wow seriously? I was convinced I use a rounding method...I'm clearly losing it!

              Link Preview Image
              Class: Numeric (Ruby 1.8.6)

              Class : Numeric - Ruby 1.8.6

              favicon

              (ruby-doc.org)

              It´s one of them methods tha´ts commonly modified. 😞

              Thomas Thomassen — SketchUp Monkey & Coding addict
              List of my plugins and link to the CookieWare fund

              1 Reply Last reply Reply Quote 0
              • genma saotomeG Offline
                genma saotome
                last edited by

                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?

                1 Reply Last reply Reply Quote 0
                • thomthomT Offline
                  thomthom
                  last edited by

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

                  Thomas Thomassen — SketchUp Monkey & Coding addict
                  List of my plugins and link to the CookieWare fund

                  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