Reputation: 6606
How do I properly escape % in call to printf in Ruby?
When I try to use %%
(as in C),
value = 47
printf "To have a 60%% test average, your final exam must be at least %0.2f.\n" % [value]
I get this error:
$ ruby /tmp/test.rb
/tmp/test.rb:4:in `printf': malformed format string - %t (ArgumentError)
from /tmp/test.rb:4:in `<main>'
It appears that the %
is being interpolated into the string before the string is checked for format arguments (i.e., the "... %% test ...
" appears to be getting interpreted as "... %test ...
"); however, I don't know the correct way to set up the format string.
Upvotes: 0
Views: 1672
Reputation: 2131
You should pass value as an argument to printf
printf "To have a 60%% test average, your final exam must be at least %0.2f.\n",value
Upvotes: 2