Perl printf问题

时间:2012-02-08 00:56:53

标签: perl printf

如何使用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;

任何人都可以帮助我吗?我确定我的作业不正确,但我只是想知道解决方案。

2 个答案:

答案 0 :(得分:6)

我认为你不应该在这里使用printf。这似乎是Perl format功能的完美应用。这些语言自成立以来一直在使用,因此Perl被认为是“实用提取和报告语言”的首字母缩写。我从未使用过格式,但您可以在本教程中了解更多信息:http://www.webreference.com/programming/perl/format/index.html

据我所知,这是一项在过去二十年中已经改变很少的功能,因此您在网络上找到的任何内容都应该为您提供有用的帮助。

答案 1 :(得分:1)

  • 您没有为大多数字段提供值。
  • 你删除了一盒星星。
  • 您将值插入到模式中。
  • 您在表单顶部添加了一个空行。

解决方案:

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,
);