• Login
sketchucation logo sketchucation
  • Login
🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

Ruby Numeric. Help - Sorry for bothering. RESOLVED

Scheduled Pinned Locked Moved Developers' Forum
1 Posts 1 Posters 162 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 30 Sept 2009, 06:50

    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
    1 / 1
    • First post
      1/1
      Last post
    Buy SketchPlus
    Buy SUbD
    Buy WrapR
    Buy eBook
    Buy Modelur
    Buy Vertex Tools
    Buy SketchCuisine
    Buy FormFonts

    Advertisement