Forum: Ruby-core [Backport 200 - Backport #7990][Open] printf and sprintf integer conversion failure of string for "0

Posted by daves (Dave Singer) (Guest)
on 2013-02-28 21:18
(Received via mailing list)
Issue #7990 has been reported by daves (Dave Singer).

----------------------------------------
Backport #7990: printf and sprintf integer conversion failure of string 
for "08" and "09"
https://bugs.ruby-lang.org/issues/7990

Author: daves (Dave Singer)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


Happens consistently. Converting var to int explicitly first or removing 
the leading "0" allows it to work fine. Example:

2.0.0-p0 :004 > (0..10).each {|x|
2.0.0-p0 :005 >     x=x.to_s
2.0.0-p0 :006?>   printf("%s,%02d;", x, x)
2.0.0-p0 :007?>   }
0,00;1,01;2,02;3,03;4,04;5,05;6,06;7,07;8,08;9,09;10,10; => 0..10
2.0.0-p0 :008 > (0..10).each {|x|
2.0.0-p0 :009 >     x = sprintf("%02d", x)
2.0.0-p0 :010?>   printf("%s,%02d;", x, x)
2.0.0-p0 :011?>   }
00,00;01,01;02,02;03,03;04,04;05,05;06,06;07,07;ArgumentError: invalid 
value for Integer(): "08"
        from (irb):10:in `printf'
        from (irb):10:in `block in irb_binding'
        from (irb):8:in `each'
        from (irb):8
        from /usr/local/rvm/rubies/ruby-2.0.0-p0/bin/irb:16:in `<main>'
2.0.0-p0 :012 > printf("%02d", "09")
ArgumentError: invalid value for Integer(): "09"
        from (irb):12:in `printf'
        from (irb):12
        from /usr/local/rvm/rubies/ruby-2.0.0-p0/bin/irb:16:in `<main>'

Running on x64 centos 6.3
#rvm list

rvm rubies

=* ruby-2.0.0-p0 [ x86_64 ]
Posted by drbrain (Eric Hodel) (Guest)
on 2013-02-28 22:57
(Received via mailing list)
Issue #7990 has been updated by drbrain (Eric Hodel).

Status changed from Open to Rejected

=begin
A number with a leading 0 is considered octal, see ((%ri 
ruby:syntax/literals%)), so this is expected behavior.

PS: This is tracker is for backports of fixed issues. You should file 
new issues here: 
https://bugs.ruby-lang.org/projects/ruby-trunk/issues/new
=end
----------------------------------------
Backport #7990: printf and sprintf integer conversion failure of string 
for "08" and "09"
https://bugs.ruby-lang.org/issues/7990#change-37202

Author: daves (Dave Singer)
Status: Rejected
Priority: Normal
Assignee:
Category:
Target version:


Happens consistently. Converting var to int explicitly first or removing 
the leading "0" allows it to work fine. Example:

2.0.0-p0 :004 > (0..10).each {|x|
2.0.0-p0 :005 >     x=x.to_s
2.0.0-p0 :006?>   printf("%s,%02d;", x, x)
2.0.0-p0 :007?>   }
0,00;1,01;2,02;3,03;4,04;5,05;6,06;7,07;8,08;9,09;10,10; => 0..10
2.0.0-p0 :008 > (0..10).each {|x|
2.0.0-p0 :009 >     x = sprintf("%02d", x)
2.0.0-p0 :010?>   printf("%s,%02d;", x, x)
2.0.0-p0 :011?>   }
00,00;01,01;02,02;03,03;04,04;05,05;06,06;07,07;ArgumentError: invalid 
value for Integer(): "08"
        from (irb):10:in `printf'
        from (irb):10:in `block in irb_binding'
        from (irb):8:in `each'
        from (irb):8
        from /usr/local/rvm/rubies/ruby-2.0.0-p0/bin/irb:16:in `<main>'
2.0.0-p0 :012 > printf("%02d", "09")
ArgumentError: invalid value for Integer(): "09"
        from (irb):12:in `printf'
        from (irb):12
        from /usr/local/rvm/rubies/ruby-2.0.0-p0/bin/irb:16:in `<main>'

Running on x64 centos 6.3
#rvm list

rvm rubies

=* ruby-2.0.0-p0 [ x86_64 ]
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.