SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    format for integers

    Hello, is it possible to define the format for integers? I need to have 3digit numbers. For instance 1 should be 001, 24 should be 024 and so on ...

    I can define an helper but maybe there is a native function.

    Thanks
    Alla prossima ...

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Integers don't have leading zeros unless its binary or octal numbers. This should be regulated to display purposes only not for internal use.

    Convert the integers to strings then pad them with a width of 3 characters. How to pad in Ruby...I don't know if its built into the String class as I've only just began to explore it.

    Edit:
    Code:
    puts "15".rjust( 3, '0' )
    => "015"
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Enthusiast shashank_hi's Avatar
    Join Date
    Apr 2005
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by macsig View Post
    Hello, is it possible to define the format for integers? I need to have 3digit numbers. For instance 1 should be 001, 24 should be 024 and so on ...

    I can define an helper but maybe there is a native function.

    Thanks
    Try this -

    Code:
    def format3Digit(number)
      return "00" + number.to_s if (number / 10) == 0
      return "0" + number.to_s if (number / 100) == 0
      return number 
    end
    Put it in a module and include it with Fixnum and Bignum!
    .

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by shashank_hi View Post
    Try this -

    Code:
    def format3Digit(number)
      return "00" + number.to_s if (number / 10) == 0
      return "0" + number.to_s if (number / 100) == 0
      return number 
    end
    Put it in a module and include it with Fixnum and Bignum!
    That just looks so inflexible...and I only starting learning Ruby... o.O

    Code Ruby:
    class Fixnum
     
      def ljust( count, sep )
        self.to_s.ljust( count, sep )
      end
     
      def rjust( count, sep )
        self.to_s.rjust( count, sep )
      end
     
      def center( count, sep )
        self.to_s.center( count, sep )
      end
     
    end
    Code:
    15.rjust( 10, '0' )
    => "0000000015"
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    metaprogramming ftw!

    Code ruby:
    class Class
      def delegate(*args)
        to = args.last.values.first
        methods = args[0..-2] + [args.last.keys.first]
        methods.each do |meth|
          define_method(meth) do |*args|
            send(to).send(meth,*args)
          end
        end
      end
    end
     
    class Fixnum
      delegate :ljust, :rjust, :center => :to_s
    end
     
    puts 4.ljust(10,'0')
    puts 4.rjust(10,'0')
    puts 4.center(10,'0')

  6. #6
    SitePoint Enthusiast AllTom's Avatar
    Join Date
    Feb 2008
    Location
    Wherever danger lies...
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think we should have stopped after logic_earth's code, though I would have written it a little differently because I think this is about more than crafting a trick to save having to type ".to_s".

    Code Ruby:
    class Fixnum
      def leading_zeroes(len = 3)
        to_s.rjust(len, "0")
      end
    end
     
    puts 4.leading_zeroes #=> "004"
    puts 4.leading_zeroes(4) #=> "0004"

    Though for style points, I might have taken the metaprogramming route. This style is a little more consistent with other Ruby code than the syntax of Fenrir2's implementation, though it only gives you one type of justification as-is.

    Code Ruby:
    class Fixnum
      alias_method :orig_to_s, :to_s
      def to_s(*args)
        options = args.detect { |x| x.is_a? Hash }
        base = orig_to_s(*(args - [options]))
        base.rjust((options && options[:leading]) || 0, '0')
      end
    end
     
    puts 4.to_s #=> "4"
    puts 4.to_s(2) #=> "100"
    puts 4.to_s(:leading => 3) #=> "004"
    puts 4.to_s(2, :leading => 8) #=> "00000100"
    There are some things you shouldn't try to code at home.

  7. #7
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    That meta programming is very interesting. I'm going have to look more into it once I get the basics of Ruby down.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  8. #8
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks,
    I solved it creating a simple helper
    Alla prossima ...

  9. #9
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's the right solution. You shouldn't even consider using metaprogramming to solve this in a real project.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •