Re: fractional cents

Uri Guttman <[email protected]>
Newsgroups gmane.comp.lang.perl.fun
Message-ID <[email protected]>
>>>>> "AP" == A Pagaltzis <[email protected]> writes:

  AP> * Bernie Cosell <[email protected]> [2004-04-19 17:37]:
  >> So: what I want is something to format money correctly, whther
  >> it is fractional or not.  there's the fairly awful:
  >> 
  >> sprintf ($amt =~ /\.\d\d\d/? "%7.3f": "%7.2f"), $amt

  AP> Test:

  AP>     perl -le'for (@ARGV) { $_ = sprintf "%7.3f", $_; \
  AP>     chop if /\.\d\d0\z/; print; }' 1.2, 1.23, 1.234, 1.2345
  AP>       1.20
  AP>       1.23
  AP>       1.234
  AP>       1.234

 perl -lpe 's/(?<=\.\d\d)(?:0\z|[^0]\d+)//'
.1234
.12
.120
.12
.123
.123

uri

-- 
Uri Guttman  ------  [email protected]  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.