Change a string to an integer, report an error if the string does not represent an integer?

Discussion in 'Ruby' started by Randy Kramer, Oct 25, 2007.

  1. Randy Kramer

    Randy Kramer Guest

    Can anybody point me to a way to check if a string represents a valid integer
    and then convert it to an integer?

    It is very likely the string will contain leading zeros, and should not
    contain any trailing non-numeric characters.

    Per the documentation (pickaxe 2), to_i won't quite do it, it will just ignore
    trailing non-numeric characters.

    Thanks!

    Randy Kramer
    Randy Kramer, Oct 25, 2007
    #1
    1. Advertising

  2. Randy Kramer

    Daniel Waite Guest

    Re: Change a string to an integer, report an error if the st

    Randy Kramer wrote:
    > Can anybody point me to a way to check if a string represents a valid
    > integer
    > and then convert it to an integer?
    >
    > It is very likely the string will contain leading zeros, and should not
    > contain any trailing non-numeric characters.
    >
    > Per the documentation (pickaxe 2), to_i won't quite do it, it will just
    > ignore
    > trailing non-numeric characters.


    match = '000345'.match(/^\d+$/) # MatchData
    match[0] # "000345"

    match = '0003b45aa'.match(/^\d+$/) # nil
    match # nil
    --
    Posted via http://www.ruby-forum.com/.
    Daniel Waite, Oct 25, 2007
    #2
    1. Advertising

  3. Randy Kramer

    Tim Hunter Guest

    Re: Change a string to an integer, report an error if the st

    Randy Kramer wrote:
    > Can anybody point me to a way to check if a string represents a valid
    > integer
    > and then convert it to an integer?
    >
    > It is very likely the string will contain leading zeros, and should not
    > contain any trailing non-numeric characters.
    >
    > Per the documentation (pickaxe 2), to_i won't quite do it, it will just
    > ignore
    > trailing non-numeric characters.
    >


    Check out Kernel#Integer
    --
    Posted via http://www.ruby-forum.com/.
    Tim Hunter, Oct 25, 2007
    #3
  4. Alle gioved=EC 25 ottobre 2007, Randy Kramer ha scritto:
    > Can anybody point me to a way to check if a string represents a valid
    > integer and then convert it to an integer?
    >
    > It is very likely the string will contain leading zeros, and should not
    > contain any trailing non-numeric characters.
    >
    > Per the documentation (pickaxe 2), to_i won't quite do it, it will just
    > ignore trailing non-numeric characters.
    >
    > Thanks!
    >
    > Randy Kramer


    Kernel.Integer can convert a string to a number raising an exception if it =
    has=20
    the wrong format. The only problem lies in the fact that it treats a string=
    =20
    with leading zeros as an octal number (for example, Integer("000123") gives=
    =20
    83). To avoid this, you can use gsub on the string:

    Integer("000123".gsub(/^0+/,''))
    =3D> 123

    I hope this helps
    Stefano Crocco, Oct 25, 2007
    #4
  5. Re: Change a string to an integer, report an error if the stringdoes not represent an integer?

    Hi --

    On Fri, 26 Oct 2007, Randy Kramer wrote:

    > Can anybody point me to a way to check if a string represents a valid integer
    > and then convert it to an integer?
    >
    > It is very likely the string will contain leading zeros, and should not
    > contain any trailing non-numeric characters.
    >
    > Per the documentation (pickaxe 2), to_i won't quite do it, it will just ignore
    > trailing non-numeric characters.


    There's a method called Integer (uppercase I and all). It blows up if
    the string has extra stuff:

    irb(main):001:0> Integer("0003")
    => 3
    irb(main):002:0> Integer("0003a")
    ArgumentError: invalid value for Integer: "0003a"


    David

    --
    Upcoming training by David A. Black/Ruby Power and Light, LLC:
    * Advancing With Rails, Edison, NJ, November 6-9
    * Advancing With Rails, Berlin, Germany, November 19-22
    * Intro to Rails, London, UK, December 3-6 (by Skills Matter)
    See http://www.rubypal.com for details!
    David A. Black, Oct 25, 2007
    #5
  6. Randy Kramer

    Jim Clark Guest

    Re: Change a string to an integer, report an error if the st

    Daniel Waite wrote:
    > match = '000345'.match(/^\d+$/) # MatchData
    > match[0] # "000345"
    >
    > match = '0003b45aa'.match(/^\d+$/) # nil
    > match # nil
    >

    Integers can also be negative. Not sure if this applies to your case but
    if so, change the regex to something like:

    match = '-000345'.match(/^\-*\d+$/) # MatchData
    match[0] # "-000345"

    match = '000345'.match(/^\-*\d+$/) # MatchData
    match[0] # "000345"

    match = '0003b45aa'.match(/^\-*\d+$/) # nil
    match # nil

    Regards,
    Jim
    Jim Clark, Oct 25, 2007
    #6
  7. Randy Kramer

    7stud -- Guest

    Randy Kramer wrote:
    > Can anybody point me to a way to check if a string represents a valid
    > integer
    > and then convert it to an integer?
    >
    > It is very likely the string will contain leading zeros, and should not
    > contain any trailing non-numeric characters.
    >
    > Per the documentation (pickaxe 2), to_i won't quite do it, it will just
    > ignore
    > trailing non-numeric characters.
    >
    > Thanks!
    >
    > Randy Kramer




    >Integers can also be negative.


    Integers can also have leading '+' signs. :)

    Another way:


    def valid_int?(str)
    start = true

    str.each_byte do |char|
    if start
    start = false
    if char == ?+ or char == ?-
    next
    end
    end

    if char < ?0 or char > ?9
    return false
    end
    end

    return true
    end

    strings = ['0013abc', '0025', '-0030', '+051', '00-1', '-abc72']
    strings.each do |str|
    if valid_int?(str)
    puts str.to_i
    else
    puts 'invalid int'
    end
    end
    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Oct 25, 2007
    #7
  8. Randy Kramer

    7stud -- Guest

    7stud -- wrote:
    > str.each_byte do |char|
    > if start
    > start = false
    > if char == ?+ or char == ?-
    > next
    > end
    > end
    >
    > if char < ?0 or char > ?9
    > return false
    > end
    > end
    >
    > return true
    > end
    >
    > strings = ['0013abc', '0025', '-0030', '+051', '00-1', '-abc72']
    > strings.each do |str|
    > if valid_int?(str)
    > puts str.to_i
    > else
    > puts 'invalid int'
    > end
    > end



    --output:--
    invalid int
    25
    -30
    51
    invalid int
    invalid int
    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Oct 25, 2007
    #8
  9. Randy Kramer

    Randy Kramer Guest

    Re: Change a string to an integer, report an error if the st

    On Thursday 25 October 2007 01:13 pm, Jim Clark wrote:
    > Daniel Waite wrote:

    ---<good stuff (in this and the other replies) omitted>----

    Thanks to all who replied!

    Randy Kramer
    Randy Kramer, Oct 25, 2007
    #9
  10. Re: Change a string to an integer, report an error if the st

    One caveat is that integers can have alpha characters in the string,
    e.g. hex.


    p '2a'.to_i(16) # => 42
    --
    Posted via http://www.ruby-forum.com/.
    Lloyd Linklater, Oct 25, 2007
    #10
  11. Randy Kramer

    7stud -- Guest

    7stud -- wrote:
    > 7stud -- wrote:
    >> str.each_byte do |char|
    >> if start
    >> start = false
    >> if char == ?+ or char == ?-
    >> next
    >> end
    >> end
    >>
    >> if char < ?0 or char > ?9
    >> return false
    >> end
    >> end
    >>
    >> return true
    >> end
    >>
    >> strings = ['0013abc', '0025', '-0030', '+051', '00-1', '-abc72']
    >> strings.each do |str|
    >> if valid_int?(str)
    >> puts str.to_i
    >> else
    >> puts 'invalid int'
    >> end
    >> end

    >
    >
    > --output:--
    > invalid int
    > 25
    > -30
    > 51
    > invalid int
    > invalid int


    Ugh. This is much faster:

    strings = ['0013abc', '0025', '-0030', '+051', '00-1', '-abc72']

    strings.each do |str|
    if str.match(/^(\-|\+)?\d+$/)
    puts str.to_i
    else
    puts "invalid int"
    end
    end
    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Oct 25, 2007
    #11
  12. Re: Change a string to an integer, report an error if the stringdoes not represent an integer?

    Hi --

    On Fri, 26 Oct 2007, 7stud -- wrote:

    > Ugh. This is much faster:
    >
    > strings = ['0013abc', '0025', '-0030', '+051', '00-1', '-abc72']
    >
    > strings.each do |str|
    > if str.match(/^(\-|\+)?\d+$/)
    > puts str.to_i
    > else
    > puts "invalid int"
    > end
    > end


    Although....

    strings = ["0025\nhello"] # will print 25

    You want \A and \z, rather than ^ and $.


    David

    --
    Upcoming training by David A. Black/Ruby Power and Light, LLC:
    * Advancing With Rails, Edison, NJ, November 6-9
    * Advancing With Rails, Berlin, Germany, November 19-22
    * Intro to Rails, London, UK, December 3-6 (by Skills Matter)
    See http://www.rubypal.com for details!
    David A. Black, Oct 25, 2007
    #12
  13. Re: Change a string to an integer, report an error if the stringdoes not represent an integer?

    On 25.10.2007 18:54, Stefano Crocco wrote:
    > Alle giovedì 25 ottobre 2007, Randy Kramer ha scritto:
    >> Can anybody point me to a way to check if a string represents a valid
    >> integer and then convert it to an integer?
    >>
    >> It is very likely the string will contain leading zeros, and should not
    >> contain any trailing non-numeric characters.
    >>
    >> Per the documentation (pickaxe 2), to_i won't quite do it, it will just
    >> ignore trailing non-numeric characters.
    >>
    >> Thanks!
    >>
    >> Randy Kramer

    >
    > Kernel.Integer can convert a string to a number raising an exception ifit has
    > the wrong format. The only problem lies in the fact that it treats a string
    > with leading zeros as an octal number (for example, Integer("000123") gives
    > 83). To avoid this, you can use gsub on the string:
    >
    > Integer("000123".gsub(/^0+/,''))
    > => 123
    >
    > I hope this helps


    I'd rather do

    Integer(s.sub(/\A([+-]?)0+(?=.)/, '\\1'))

    because your regexp has some issues:

    1. "+010" is unchanged and will yield 8 instead of 10

    2. same for negative numbers

    3. "00" will be converted to "" which will trigger an error

    4. you use gsub although the regular expression can match only once

    Subtle, subtle...

    Kind regards

    robert
    Robert Klemme, Oct 25, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?ZG90bmV0dGVzdGVy?=

    dll does not represent a strongly named assembly

    =?Utf-8?B?ZG90bmV0dGVzdGVy?=, Oct 20, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    5,559
    =?Utf-8?B?ZG90bmV0dGVzdGVy?=
    Oct 20, 2005
  2. Steve
    Replies:
    2
    Views:
    2,036
    Victor Bazarov
    Feb 13, 2005
  3. Anupam  Kapoor

    what does a c program really represent

    Anupam Kapoor, Mar 6, 2006, in forum: C Programming
    Replies:
    5
    Views:
    255
    CBFalconer
    Mar 7, 2006
  4. Phil Ward
    Replies:
    5
    Views:
    328
    Kaz Kylheku
    Nov 2, 2006
  5. Yaxm Yaxm
    Replies:
    6
    Views:
    122
    Robert Dober
    May 1, 2007
Loading...

Share This Page