Daniel Martin
8/18/2006 6:01:00 PM
Carlos <angus@quovadis.com.ar> writes:
> ara.t.howard@noaa.gov wrote:
>
>> On Sat, 19 Aug 2006, Wes Gamble wrote:
>>
>> so it's expected, documented, and consistent with c.
>
> It is not expected, nor documented, because the documentation of
> sprintf says:
> d | Convert argument as a decimal number.
>
> ...which is very explicit in that the argument will be interpreted as
> a decimal number, not octal.
I think that the problem here is that ruby is performing two
conversions, and the sprintf documentation only mentions one of them.
When you use '%d' in a sprintf(), ruby does a conversion of the
argument to an Integer using Kernel#Integer and then does the
conversion of the Integer to a portion of the output string as
documented.
Similarly, when you use %f in a sprintf(), ruby does a conversion of
the argument using Kernel#float and then does the conversion from
Float to part of the output string as documented.
So, we have:
sprintf("%d",'08') ==> ArgumentError
sprintf("%d",'8') ==> "8"
sprintf("%d",'08'.to_i) ==> "8"
sprintf("%f",'08') ==> "8.000000"
I think that if you're hitting this quirk in actual code, your code
needs so .to_i dropped in a few places. I'm actually surprised ruby
does those String=>Integer conversions at all.