pretty-printing of monetary fields 
Author Message
 pretty-printing of monetary fields

Before anyone invokes the evil name of C*B*L, I'd really like to be able
to
print dollar fields with internal comma separators.  Somehow, it is hard
for my eyes to instantaneously pick out the difference between 29975.00
and 299975.00, especially if they are not aligned.

Have I just overlooked something in the camel book?
--

Steve

--
Stephen C. Hill, CDP

Integration Products Group
MIPS Computer Systems, Stop 6-03
930 Deguigne Avenue
Sunnyvale, CA 94086, (408) 524-7436         Fax (408) 524-7528

Time is Nature's method of keeping us from bumping into ourselves.



Sat, 17 Dec 1994 12:44:51 GMT  
 pretty-printing of monetary fields

:Before anyone invokes the evil name of C*B*L, I'd really like to be able
:to
:print dollar fields with internal comma separators.  Somehow, it is hard
:for my eyes to instantaneously pick out the difference between 29975.00
:and 299975.00, especially if they are not aligned.
:
:Have I just overlooked something in the camel book?

From page 233 of the Book:

    sub commas {
        local($_) = shift;
        1 while s/(.*\d)(\d{3}/$1,$2/;
        $_;
    }

    print &commas(299975.00), "\n";
    299,975.00

    print &commas(29975.00), "\n";
    29,975.00

--tom
--

    If mathematically you end up with the wrong answer, try multiplying by
    the page number.



Sat, 17 Dec 1994 22:11:16 GMT  
 pretty-printing of monetary fields

Quote:

>:Before anyone invokes the evil name of C*B*L, I'd really like to be able
>:to
>:print dollar fields with internal comma separators.  Somehow, it is hard
>:for my eyes to instantaneously pick out the difference between 29975.00
>:and 299975.00, especially if they are not aligned.
>:
>:Have I just overlooked something in the camel book?

>From page 233 of the Book:

>    sub commas {
>    local($_) = shift;
>    1 while s/(.*\d)(\d{3}/$1,$2/;

        1 while s/(.*\d)(\d{3})/$1,$2/;
                              ^ typo, I presume

Quote:
>    $_;
>    }

>    print &commas(299975.00), "\n";
>    299,975.00

     299,975

     print &commas("299975.00"), "\n";
     299,975.00

Quote:

>    print &commas(29975.00), "\n";
>    29,975.00

     29,975

     print &commas("29975.00"), "\n";
     29,975.00

     print &commas(29975.0000), "\n";
     29,975

     print &commas("29975.0000"),, "\n";
     29,975.0,000
             ^ Hmmm...

     print &commas(299.970542), "\n";
     299.97,054,200,000,002,311
           ^^^^^^^^^^^^^^^^^  Wow!

     The Book does say on page 233 that this is only for "integers": "If EXPR is
     not an integer, &commas may produce incorrect output,..." so using
     "299975.00" is a little misleading.

     If you use the following, you get a little better behavior:
     sub commas {
        local($_) = shift;
        $_ .= '.' if ! /\./;            # well formed on right
        1 while s/(.*\d)(\d{3})([.,].*)/$1,$2$3/;
        s/\.$//;                        # no orphan period
        $_;
     }

     print &commas(299975.00), "\n";
     299,975

     print &commas("299975.00"), "\n";
     299,975.00

     print &commas(29975.00), "\n";
     29,975

     print &commas("29975.00"), "\n";
     29,975.00

     print &commas(29975.0000), "\n";
     29,975

     print &commas("29975.0000"), "\n";
     29,975.0000

     print &commas(29975), "\n";
     29,975

     print &commas("29975"), "\n";
     29,975

     print &commas(299.970542), "\n";
     299.97054200000002311

     print &commas("299.970542"), "\n";
     299.970542

Quote:

>--tom
>--

>    If mathematically you end up with the wrong answer, try multiplying by
>    the page number.

TD
---
Tom Dinger           consulting at: Sun Technology Enterprises, Inc.
TechnoLogics, Inc.                  (A Sun Microsystems, Inc. company)
(508)486-8500                       (508)671-0521



Sun, 18 Dec 1994 05:00:05 GMT  
 pretty-printing of monetary fields
Stephen> I'd really like to be able to print dollar fields with
Stephen> internal comma separators.

################################################################
#
# Make a number comma'd
#
# eg 123456.78 ->    123,456.78
sub comma {
    local($v) = shift;
    1 while $v =~ s/(\d)(\d{3})([.,]|$)/$1,$2$3/;
    $v;

Quote:
}

################################################################
#
# make the value into dollars and cents.
# e.g. 1234.56 -> $1,234.56
#
sub dollars {
    local($m) = sprintf("%.2f", $_[0]);
    1 while $m =~ s/(\d)(\d{3})([.,]|$)/$1,$2$3/;
    '$' . $m;
Quote:
}

--




Sun, 18 Dec 1994 14:00:40 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. WANTED: module that pretty-prints complex data structures

2. automatically pretty print columns of data

3. How to print pretty with XML::DOM??

4. tool to pretty print directory trees?

5. Perl pretty-printing

6. pretty print in PERL

7. Pretty printing arbitrary nested arrays.

8. Pretty-printing CSS

9. pretty-print perl code?

10. pretty printing a web page

11. pretty-printing perl?

12. pretty print cron?

 

 
Powered by phpBB® Forum Software