Re: [Templates] Putting commas in money values

[prev] [thread] [next] [Date index for 2004/08/20]

From: Dave Cash
Subject: Re: [Templates] Putting commas in money values
Date: 03:13 on 20 Aug 2004
On Thu, 19 Aug 2004, Buddy Burden wrote:

> Okay, the canonical MACRO example goes something like:
>
> 	[% MACRO commify(n) GET n.chunk(-3).join(',') %]

It ain't pretty, but how about:

	[% MACRO commify(n) GET n.match('^([^.]+)').0.chunk(-3).join(',') _ n.match('(\..+)$').0 %]

or for European numbers:

	[% MACRO dotify(n) GET n.match('^([^,]+)').0.chunk(-3).join('.') _ n.match('(\,.+)$').0 %]

> Simple enough.  But what if you have decimal places on the end?  I mean,
> I know I can do this the hard way, but I keep feeling like I'm missing
> something.  Is there some easy way, or maybe existing
> plugin/filter/predefined macro somewhere?  I'm sure it's a problem
> others have faced before me.

OK, maybe my suggestion's the hard way.  But it was fun to write!

Hope that helps,

Dave

P.S. For the record, this was my first try:

	[% MACRO commify(n) BLOCK; GET n.split('\.').0.chunk(-3).join(','); GET '.' _ n.split('\.').1 IF n.match('\.').0; END %]

I didn't benchmark to see which was faster.

/L\_/E\_/A\_/R\_/N\_/T\_/E\_/A\_/C\_/H\_/L\_/E\_/A\_/R\_/N\
Dave Cash                              Power to the People!
Frolicking in Fields of Garlic               Right On-Line!
dave@xxxxx.xxx                                  Dig it all.

_______________________________________________
templates mailing list
templates@xxxxxxxxxxxxxxxx.xxx
http://lists.template-toolkit.org/mailman/listinfo/templates

[Templates] Putting commas in money values
Buddy Burden 21:16 on 19 Aug 2004

Re: [Templates] Putting commas in money values
Dave Cash 03:13 on 20 Aug 2004

Re: [Templates] Putting commas in money values
Mark Fowler 09:22 on 20 Aug 2004

Re: [Templates] Putting commas in money values
Buddy Burden 14:29 on 20 Aug 2004

Re: [Templates] Putting commas in money values
Chris Ochs 16:24 on 23 Aug 2004

Generated at 08:56 on 15 Mar 2005 by mariachi v0.52