round in Smarty shows wrong result

Yekver picture Yekver · Jul 31, 2013 · Viewed 22.5k times · Source

In my Smarty 3 template I have this code:

{$a=8.34}
{$b=8.33}
{$a-$b|round:2}

Expected result is: 0.01

But I receive this: 0.0099999999999998

Does anybody know how to fix this?

Answer

Jean-Paul picture Jean-Paul · Jul 31, 2013

Smarty2 applied the modifier to the result of the complete expression.

Smarty3 does it just on the direct prepending value.

So in Smarty3 you have to use brackets:

{($a-$b)|round:2} 

That should solve it.