Reputation: 11
How do I print this example output using 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: ? **
** **
******************************************************************************
I tried my way, but I am not sure if I am right:
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;
Can anyone help me? I'm sure I turned in my assignment incorrect, but I just want to know the solution.
Upvotes: 1
Views: 332
Reputation: 385655
Solution:
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,
);
Upvotes: 1
Reputation: 1047
I don't think you should use printf here. This seems like a perfect application for Perl's format
capabilities. These have been in the language since its inception and are why Perl is supposedly an acronym for "Practical Extraction and Reporting Language". I have never used formats, but you can learn more at this tutorial: http://www.webreference.com/programming/perl/format/index.html
To the best of my knowledge, this is a feature that has changed very little over the last two decades, so just about anything you find on the web should give you useful help.
Upvotes: 6