tags:

views:

365

answers:

6

Simple problem that I can't figure out...

How can I print a '%' character within a printf string? The code below prints it, but gives an 'invalid conversion' error as well.

printf "\t\t".$hour."00 HRS\t=>\t%.2f\t%.2f\t%.1f\%\n", $total, $max15, ($max15/$total*100);

Should output something like:

        0000 HRS    =>    3125.19    898.02    28.7%
+13  A: 

You would use %%, not \% (from man printf)

mirod
+1 for winning the speed race :)
Vinko Vrsalovic
Does "man printf" really tell you about the Perl function on your system? On mine, it only tells about the command-line program. I can get the C function with "man 3 printf"; if I want to read about the Perl function, I use "perldoc -f printf"
Rob Kennedy
At least it looks like we all agree on the answer.
mirod
@Rob Kennedy: "perldoc -f printf" doesn't give me any detail but instead refers me to sprintf. "perldoc -f sprintf" indeed gives me all the details I need, much like "man printf". "man 3 printf" OTOH gives me the man page for an OCaml library. That's on a recent kUbuntu (with OCaml installed obviously!)
mirod
@mirod: Thanks for pointing me to the resource as well.
Zaid
+3  A: 

Instead of \% use %% :)

l3dx
+2  A: 

Use %% to print a single %

printf "\t\t".$hour."00 HRS\t=>\t%.2f\t%.2f\t%.1f%%\n", $total, $max15, ($max15/$total*100);
Vinko Vrsalovic
+4  A: 

%% for a single %

joe
+1  A: 

On hindsight, there was a crude alternative which would have done the same thing.

Print the '%' symbol as a string:

printf "\t\t".$hour."00 HRS\t=>\t%.2f\t%.2f\t%.1f%s\n", $total, $max15, ($max15/$total*100), "%";
Zaid
plus-1 for being sly ;)
tenpn
A: 

This is a bit tricky because the documentation for the template for printf is actually in the documentation for sprintf. You have to catch that line in the middle of the paragraph to know to look there.

brian d foy