sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    🫛 Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

    Ruby Numeric. Help - Sorry for bothering. RESOLVED

    Scheduled Pinned Locked Moved Developers' Forum
    1 Posts 1 Posters 174 Views 1 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.
    • N Offline
      NewOne
      last edited by

      I have to display some values, and most of all are integers, but some will be float. This is why I must use float.
      I want to get rid of zero to that values that don't require it.
      This is what I want:

      %(#4000BF)[3.3 => 3.3
      4.8 => 4.8]
      16.0 => 16

      I could do the trick in many ways but I'm still looking for something native.

      There is no native way, but I find this on web:

      
      #extend the Float class
      class Float
        def to_i_if
          if self - floor == 0
            floor
          else
            self
          end
        end
      end
      

      But here will be a problem: if I have a number like this: 4.00001, won't be converted to integer. For that, the method to_i_if can be enhanced:

      
      class Float
        def to_i_if(threshold=0)
          if self - floor <= threshold
            floor
          else
            self
          end
        end
      end
      
      

      treshold => display format like this: 0.01 (how many zeroes you need)

      I found this on web and I thought to share it.

      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