Question

I need a value to be rounded in the Perl's Template Toolkit. But I am not able to use ceil().

[%interestRate = ceil(mortgage.interest_rate / 100)%]

The answer shows a null value.

Was it helpful?

Solution 3

I think the syntax to provide ceil is

$c->stash->{ceil} = sub { ceil($_[0]) };

[% c.ceil(c.mortgage.interest_rate / 100) %]

But its usually better to do your calculations outside of templates.

$c->stash->{mortgagetInterestRate} = ...;

OTHER TIPS

Maybe you need division directive:

[% 15 / 6 %] -> 2.5

[% 15 div 6 %] -> 2

[% 15 mod 6 %] -> 3

The div operator returns the integer result of division. Both % and mod return the modulus (i.e. remainder) of division.

http://www.template-toolkit.org/docs/manual/Directives.html

If you prefer a CPAN module, then look into Template::Plugin::POSIX. This module provides amongst others the ceil and floor functions:

[% USE POSIX -%]
[% POSIX.ceil(0.5) %]
[% POSIX.floor(0.5) %]

Output:

1
0

If last digit do not matter:

[% number FILTER format("%.2f"); %]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top