Thanks, I ended up going with
[% ((pc.disk_total - pc.disk_free) / pc.disk_total) * 100 FILTER format('%02.2f') %]
(FILTER at the end so it matches the syntax I use elsewhere). I really did prefer to keep it all in the template, 'cause as you said, it's a display thing, not a backend thing.
-----Original Message-----
From: darren chamberlain [mailto:[EMAIL PROTECTED]]
Sent: Monday, November 26, 2001 4:44 PM
To: '[EMAIL PROTECTED]'
Subject: Re: [Templates] Math and Rounding in a template.
JACOB STEENHAGEN <[EMAIL PROTECTED]> said something to this effect on 11/26/2001:
> I have the following fragment in a template I use, and this
> works great except for the fact that I don't seem to be able to
> figure out how to round the result. In one example, this
> returns 11.0371741632061. I'd like it to return that number
> rounded to the nearest tenth or if it's considerably easier,
> just the int() but I can't figure out how to (without doing the
> math beforehand in perl). In case you aren't sure, this is to
> give the percentage of disk space used.
>
> [% IF pc.disk_total %][% ((pc.disk_total - pc.disk_free) /
> pc.disk_total) * 100 %][% ELSE %]Err[% END %]
Try the format filter:
[% IF pc.disk_total %]
[% FILTER format('%02.2f');
((pc.disk_total - pc.disk_free) / pc.disk_total) * 100;
END %]
[% ELSE %]
Err
[% END %]
Here is my "test":
$ perl -MTemplate
my $b = q#[% IF pc.disk_total %]
[% FILTER format('%02.2f');
((pc.disk_total - pc.disk_free) / pc.disk_total) * 100;
END %]
[% ELSE %]
Err
[% END %]
#;
my $t = Template->new(TRIM => 1);
$t->process(\$b, { pc => { disk_total => 100, disk_free => 45.456754 } }) or die $t->error;
Which yields 54.54, which looks correct to me.
(darren)
--
Sigmund Freud is alleged to have said that in the last analysis the
entire field of psychology may reduce to biological electrochemistry.
_______________________________________________
templates mailing list
[EMAIL PROTECTED]
http://www.template-toolkit.org/mailman/listinfo/templates
