How do I print the '%' character with '

2020-02-13 09:43发布

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%

标签: perl printf
6条回答
趁早两清
2楼-- · 2020-02-13 09:57

Use %% to print a single %

printf "\t\t".$hour."00 HRS\t=>\t%.2f\t%.2f\t%.1f%%\n", $total, $max15, ($max15/$total*100);
查看更多
▲ chillily
3楼-- · 2020-02-13 10:05

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

查看更多
来,给爷笑一个
4楼-- · 2020-02-13 10:06

%% for a single %

查看更多
Rolldiameter
5楼-- · 2020-02-13 10:06

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.

查看更多
小情绪 Triste *
6楼-- · 2020-02-13 10:12

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), "%";
查看更多
仙女界的扛把子
7楼-- · 2020-02-13 10:16

Instead of \% use %% :)

查看更多
登录 后发表回答