2008/05/16

The Labs.Com Issue_08_Unreal
Last update 1999/02/20

TPJ: Issue_08_Unreal

This is a collection of programs published by The Perl Journal. You can download all source-code also from TPJ: Programs.
  1. problem
  2. solution
  3. More Samples on Unreal
Issue_08_Unreal
1. problem
Download problem

 #!/usr/bin/perl -w 
 # See whether Perl can add? 
 use strict; 
 my $total = 19.08 + 2.01;  
 my $expected_total = 21.09; 
       
 print "The total is \$$total\n";  
 print "The expected total was \$$expected_total\n"; 
 if ($total == $expected_total) { 
     print "They are equal.\n";  
 } else { 
     print "They aren't equal.\n";  
 } 

Issue_08_Unreal
2. solution

Download solution

 #!/usr/bin/perl -w 
 # A better way to work with financial values 
 use strict; 
 sub money ($) { 
     # Given a non-negative integer number of pennies, 
     # returns that figure as a dollars-and-cents amount,  
     # with dollar sign, something like '$12345.67'. 
     my $num = shift; 
     warn "Bad amount of money: $num" if $num < 0; 
  
     $num = sprintf "%03d", $num;        # make it a string 
     substr($num, -2, 0) = ".";        # add the decimal point 
     '$' . $num;                        # return value 
 } 
 my $total = 1908 + 201;                # integers now 
 my $expected_total = 2109; 
  
 print "The total is ", money($total), "\n";  
 print "The expected total was ", money($expected_total), "\n"; 
 if ($total == $expected_total) {        # Comparing numbers of pennies 
     print "They are equal.\n"; 
 } else { 
     print "They aren't equal.\n";  
 } 

Issue_08_Unreal
3. More Samples on Unreal

  • Issue_08_Unreal

                                                                                                                                   

Last update 1999/02/20

All Rights Reserved - (C) 1997 - 2008 by The Labs.Com

Top of Page

The Labs.Com