4

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.

ikegami
  • 367,544
  • 15
  • 269
  • 518
Jitesh
  • 345
  • 1
  • 5
  • 16

4 Answers4

8

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

Matthew Lock
  • 13,144
  • 12
  • 92
  • 130
Zet
  • 81
  • 1
  • 1
6

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
Slaven Rezic
  • 4,571
  • 14
  • 12
5

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} = ...;
ikegami
  • 367,544
  • 15
  • 269
  • 518
1

If last digit do not matter:

[% number FILTER format("%.2f"); %]
Machavity
  • 30,841
  • 27
  • 92
  • 100