Perl printf问题

Perl printf问题,perl,printf,Perl,Printf,如何使用printf打印此示例输出 ****************************************************************************** ** XYZ Corporation Date: March 27, 1989(Use current date)** ** 999 John Doe street

如何使用printf打印此示例输出

******************************************************************************
**  XYZ Corporation                   Date: March 27, 1989(Use current date)**
**  999 John Doe street                                                     **
**  Ypsilanti, MI. 48197.                                                   **
**                                                                          **
**  Pay to the order of:  ?                                                 **
**             The amount of: ?     dollars, and    ?    cents              **
**                                                                          **
**                                 signed:                                  **
**                                         President, XYZ Corporation.      **
**                                                                          **
**--------------------------------------------------------------------------**
**                          SUMMARY                                         **
**  Social security number:   ?                                             **
**  Regular pay:              ?                                             **
**  Overtime pay:             ?                                             **
**  Gross pay:                ?                                             **
**  Federal tax:              ?                                             **
**  Social sec. deduction:    ?                                             **
**  City tax:                 ?                                             **
**  Union dues:               ?                                             **
**  Net pay:                  ?                                             **
**                                                                          **
******************************************************************************
我尝试了我的方法,但我不确定我是否正确:

printf"
XYZ Corporation                   Date: 
999 John Doe street                                                     
Ypsilanti, MI. 48197                                                   

  Pay to the order of:  |                                                 
  The amount of: |     dollars, and    |    cents              

                                 signed:                                  
                                         President, XYZ Corporation.      

--------------------------------------------------------------------------
                          SUMMARY                                         
  Social security number:   $ssn                                           
  Regular pay:              %-.2f
  Overtime pay:             %-.2f
  Gross pay:                %-.2f
  Federal tax:              %-.2f
  Social sec. deduction:    %-.2f
  City tax:                 %-.2f
  Union dues:               %-.2f
  Net pay:                  %-.2f\n", $regPay, $overPay, $grossPay, $fedTax, $ssnDeduction, $cityTax, $unionDues, $netPay;

有人能帮我吗?我肯定我交的作业不正确,但我只是想知道解决办法。

我认为你不应该在这里使用printf。这似乎是Perl的
格式
功能的完美应用程序。这些都是自Perl诞生以来就一直使用的语言,这就是为什么Perl被认为是“实用提取和报告语言”的首字母缩略词。我从未使用过格式,但您可以在本教程中了解更多内容:


据我所知,这是一项在过去二十年中变化不大的功能,因此您在web上找到的任何东西都会给您提供有用的帮助。

我认为您不应该在这里使用printf。这似乎是Perl的
格式
功能的完美应用程序。这些都是自Perl诞生以来就一直使用的语言,这就是为什么Perl被认为是“实用提取和报告语言”的首字母缩略词。我从未使用过格式,但您可以在本教程中了解更多内容:

据我所知,这是一项在过去二十年中变化不大的功能,因此您在web上找到的任何东西都会给您提供有用的帮助。

  • 您没有为大多数字段提供值
  • 你拿走了那盒星星
  • 将值插值到图案中
  • 您在表单顶部添加了一个空行
解决方案:

use POSIX qw( strftime );

my $date = strftime("%B %d, %Y", localtime);

# Doing it this way prevents floating point rounding errors.
my $net_pay_x100    = sprintf("%.0f", $net_pay * 100);
my $net_pay_cents   = $net_pay_x100 % 100;
my $net_pay_dollars = ( $net_pay_x100 - $net_pay_cents ) / 100;

printf(<<'__EOI__',
******************************************************************************
**  XYZ Corporation                   Date: %-31s **
**  999 John Doe street                                                     **
**  Ypsilanti, MI. 48197.                                                   **
**                                                                          **
**  Pay to the order of: %-50s **
**             The amount of: %5d dollars, and %02d cents                   **
**                                                                          **
**                                 signed:                                  **
**                                         President, XYZ Corporation.      **
**                                                                          **
**--------------------------------------------------------------------------**
**                          SUMMARY                                         **
**  Social security number: %11s                                     **
**  Regular pay:            %7.2f                                         **
**  Overtime pay:           %7.2f                                         **
**  Gross pay:              %7.2f                                         **
**  Federal tax:            %7.2f                                         **
**  Social sec. deduction:  %7.2f                                         **
**  City tax:               %7.2f                                         **
**  Union dues:             %7.2f                                         **
**  Net pay:                %7.2f                                         **
**                                                                          **
******************************************************************************
__EOI__
   $date,
   $name,
   $net_pay_dollars,
   $net_pay_cents,
   $ssn,
   $reg_pay,
   $over_pay,
   $gross_pay,
   $fed_tax,
   $ssn_deduction,
   $city_tax,
   $union_dues,
   $net_pay,
);
使用POSIXQW(strftime);
my$date=strftime(“%B%d,%Y”,localtime);
#这样做可以防止浮点舍入错误。
我的$net\u pay\u x100=sprintf(%.0f),$net\u pay*100);
我的$net_pay_cents=$net_pay_x100%100;
我的$net\u pay\u美元=($net\u pay\u x100-$net\u pay\u cents)/100;
printf(
  • 您没有为大多数字段提供值
  • 你拿走了那盒星星
  • 将值插值到图案中
  • 您在表单顶部添加了一个空行
解决方案:

use POSIX qw( strftime );

my $date = strftime("%B %d, %Y", localtime);

# Doing it this way prevents floating point rounding errors.
my $net_pay_x100    = sprintf("%.0f", $net_pay * 100);
my $net_pay_cents   = $net_pay_x100 % 100;
my $net_pay_dollars = ( $net_pay_x100 - $net_pay_cents ) / 100;

printf(<<'__EOI__',
******************************************************************************
**  XYZ Corporation                   Date: %-31s **
**  999 John Doe street                                                     **
**  Ypsilanti, MI. 48197.                                                   **
**                                                                          **
**  Pay to the order of: %-50s **
**             The amount of: %5d dollars, and %02d cents                   **
**                                                                          **
**                                 signed:                                  **
**                                         President, XYZ Corporation.      **
**                                                                          **
**--------------------------------------------------------------------------**
**                          SUMMARY                                         **
**  Social security number: %11s                                     **
**  Regular pay:            %7.2f                                         **
**  Overtime pay:           %7.2f                                         **
**  Gross pay:              %7.2f                                         **
**  Federal tax:            %7.2f                                         **
**  Social sec. deduction:  %7.2f                                         **
**  City tax:               %7.2f                                         **
**  Union dues:             %7.2f                                         **
**  Net pay:                %7.2f                                         **
**                                                                          **
******************************************************************************
__EOI__
   $date,
   $name,
   $net_pay_dollars,
   $net_pay_cents,
   $ssn,
   $reg_pay,
   $over_pay,
   $gross_pay,
   $fed_tax,
   $ssn_deduction,
   $city_tax,
   $union_dues,
   $net_pay,
);
使用POSIXQW(strftime);
my$date=strftime(“%B%d,%Y”,localtime);
#这样做可以防止浮点舍入错误。
我的$net\u pay\u x100=sprintf(%.0f),$net\u pay*100);
我的$net_pay_cents=$net_pay_x100%100;
我的$net\u pay\u美元=($net\u pay\u x100-$net\u pay\u cents)/100;
printf(我听说Perl6样式表单用于Perl5)比Perl5的内置格式.YMMV好得多。我听说(Perl6样式表单用于Perl5)比Perl5的内置格式.YMMV好得多。