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.
The string of characters is interpreted as a decimal number IF IT IS
one. Since you started it with a zero, it’s octal.
It’s a string… I wanted this string formatted as a decimal number.
Just
put these digits right justified in that other string. All this
behind-the-scenes conversion to integer is an implementation detail I
should
not care about.
For that conversion, #to_i could have been used as well, and then ‘09’
would
be interpreted as decimal 9. Why should I expect my string to be passed
through Integer() before formatting? That is not documented.
So, I expected
sprintf “%02d”, “09” # => “09”
I see that none of you (except Wes) expected that, so, OK, my
expectations
should be wrong. I really didn’t expect that (but I’m bad on
expectations
:), I supposed that my expectations would be the majority, or that at
least 50% would expect something like #to_i.
But everyone expected Integer() and Integer() is what is used, and this
took
nobody by surprise, so it’s OK, I guess
anyway?
Actually, let me say this, as it’s more informative and
constructive.
If you need to convert your string to a number, it’s best
to do so explicitly. (If you really want to pass a string
into sprintf, %s is the correct modifier.)
Also note that while Integer() doesn’t convert the way
you want, to_i does:
Also note that while Integer() doesn’t convert the way
Hal
all you are saying is true BUT
I do not think we should blame C for something which really is not nice.
I know not many share my opinion but the brave speaks out nevertheless
IMHO the interpreter should raise an expression when a String is passed
to
%d (as you asked OP rightfully not to do so).
Early Failure, please, early.
BTW who is talking about sprintf I am talking about “%” a very rubish
construct.
Cheers
Robert
–
Deux choses sont infinies : l’univers et la bêtise humaine ; en ce qui
concerne l’univers, je n’en ai pas acquis la certitude absolue.
Hal
all you are saying is true BUT
I do not think we should blame C for something which really is not nice.
I know not many share my opinion but the brave speaks out nevertheless
IMHO the interpreter should raise an expression when a String is passed to
%d (as you asked OP rightfully not to do so).
Early Failure, please, early.
I see your point. Truthfully, I think automatic conversion of a
string in this case is probably a bad idea. And that is a Ruby
issue, not a C one.
BTW who is talking about sprintf I am talking about “%” a very rubish
construct.
But format % array behaves the same as sprintf(format,*array)
The specifiers are the same (and the same as C, perhaps with a few
minor differences).
to
But format % array behaves the same as sprintf(format,*array)
Yup, I failed to be clear about this sorry for wasting Austin’s and your
time.
I should have said
even more so as we have the % operator which looking very rubish
still
has these
C caveats.
Anyway I am more than happy that you share my POV
Cheers
Robert
The specifiers are the same (and the same as C, perhaps with a few