How do I print the '%' character with 'printf'?

前端 未结 6 699
离开以前
离开以前 2020-12-20 14:58

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 co

相关标签:
6条回答
  • 2020-12-20 15:31

    Use %% to print a single %

    printf "\t\t".$hour."00 HRS\t=>\t%.2f\t%.2f\t%.1f%%\n", $total, $max15, ($max15/$total*100);
    
    0 讨论(0)
  • 2020-12-20 15:32

    Instead of \% use %% :)

    0 讨论(0)
  • 2020-12-20 15:34

    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), "%";
    
    0 讨论(0)
  • 2020-12-20 15:42

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

    0 讨论(0)
  • 2020-12-20 15:50

    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.

    0 讨论(0)
  • 2020-12-20 15:52

    %% for a single %

    0 讨论(0)
提交回复
热议问题