[comp.lang.perl] Is this how formats are supposed to work?

clewis@eci386.uucp (Chris Lewis) (01/16/90)

Is there any way to set the display format for numbers in format lines?
Assigning "%.2g" to $# (after selecting the handling associated with the
stream) doesn't appear to affect anything and things are printed with
more than 2 decimal places.
-- 
Chris Lewis, Elegant Communications Inc, {uunet!attcan,utzoo}!lsuc!eci386!clewis
Ferret mailing list: eci386!ferret-list, psroff mailing list: eci386!psroff-list

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (01/16/90)

In article <1990Jan15.185922.20098@eci386.uucp> clewis@eci386 (Chris Lewis) writes:
: Is there any way to set the display format for numbers in format lines?

No.

: Assigning "%.2g" to $# (after selecting the handling associated with the
: stream) doesn't appear to affect anything and things are printed with
: more than 2 decimal places.

Maybe it should work this way--I haven't thought about it.  Or maybe we
need numeric "pictures".

For the time being, just slap an sprintf around the variable name.

Larry

jfh@rpp386.cactus.org (John F. Haugh II) (01/17/90)

In article <6798@jpl-devvax.JPL.NASA.GOV> lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes:
I-In article <1990Jan15.185922.20098@eci386.uucp> clewis@eci386 (Chris Lewis) writes:
n-: Is there any way to set the display format for numbers in format lines?
e-
w-No.
s-
--: Assigning "%.2g" to $# (after selecting the handling associated with the
n-: stream) doesn't appear to affect anything and things are printed with
o-: more than 2 decimal places.
i-
s-Maybe it should work this way--I haven't thought about it.  Or maybe we
e-need numeric "pictures".

Perhaps a function "picture" that takes two arguments - the first a
number, either floating point or integer, and a picture string.  The
function formats the number according to the picture string.

[ Anyone familiar with INFORMIX[tm] RDBMS can quit now ... ]

The format characters would be

	< - left justify numeric
	# - blank-fill numeric
	& - zero-fill numeric
	* - star-fill numeric [ for bank drafts, etc ]
	, - literal comma, unless a space is to the left
	. - literal decimal point
	$ - dollar sign in first blank left of number
	() - show sign ala accounting statements
	- - [ minus sign ] blank for positive, '-' for negative
	+ - '+' for positive, '-' for negative

The values and pictures below would give the following results -

	    value        "$<,<<&.##"   "#,###.##"  "*,***.##"  "&&&&"
	     0.01     -> "$0.01"       "     .01"  "*****.01"  "0000"
	     0.12     -> "$0.12"       "     .12"  "*****.12"  "0000"
	     1.23     -> "$1.23"       "    1.23"  "****1.23"  "0001"
	    12.34     -> "$12.34"      "   12.34"  "***12.34"  "0012"
	   123.45     -> "$123.45"     "  123.45"  "**123.45"  "0123"
	  1234.56     -> "$1,234.56"   "1,234.56"  "1,234.56"  "1234"
-- 
John F. Haugh II                             UUCP: ...!cs.utexas.edu!rpp386!jfh
Ma Bell: (512) 832-8832                           Domain: jfh@rpp386.cactus.org

clewis@eci386.uucp (Chris Lewis) (01/18/90)

In article <6798@jpl-devvax.JPL.NASA.GOV> lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes:
> In article <1990Jan15.185922.20098@eci386.uucp> clewis@eci386 (Chris Lewis) writes:
 
| > [regarding trying to set numeric display formats, and $# doesn't work.]
| : Assigning "%.2g" to $# (after selecting the handling associated with the
| : stream) doesn't appear to affect anything and things are printed with
| : more than 2 decimal places.
 
| Maybe it should work this way--I haven't thought about it.  Or maybe we
| need numeric "pictures".

If I had my druthers (then again, it's your code not mine ;-), I'd prefer 
numeric pictures, and *not* have $# take effect.  Numeric pictures have
the risk of turning Perl into Cobol (yuck ;-), but a small increment would
greatly improve perl's use in a variety of things - primarily decimal point
placement.  Eg: @>>>>>>.>>
 
| For the time being, just slap an sprintf around the variable name.

That's what I did.

Thank you!  (for this, and everything else)
-- 
Chris Lewis, Elegant Communications Inc, {uunet!attcan,utzoo}!lsuc!eci386!clewis
Ferret mailing list: eci386!ferret-list, psroff mailing list: eci386!psroff-list

allbery@NCoast.ORG (Brandon S. Allbery) (01/19/90)

As quoted from <17678@rpp386.cactus.org> by jfh@rpp386.cactus.org (John F. Haugh II):
+---------------
| Perhaps a function "picture" that takes two arguments - the first a
| number, either floating point or integer, and a picture string.  The
| function formats the number according to the picture string.
+---------------

I already have PD code to do this, if someone would like a starting point.

++Brandon
-- 
Brandon S. Allbery    allbery@NCoast.ORG, BALLBERY (MCI Mail), ALLBERY (Delphi)
      uunet!cwjcc.cwru.edu!ncoast!allbery ncoast!allbery@cwjcc.cwru.edu
*(comp.sources.misc mail to comp-sources-misc[-request]@backbone.site, please)*
*Third party vote-collection service: send mail to allbery@uunet.uu.net (ONLY)*