2010/03/19

smarty dollar to cent

In a project we have a smarty variable presenting a dollar value like ‘1.23’, or ‘.06’. And we wanted to show cent value when the value less than one dollar, for exmaple,

  1. “.78” will be “78”
  2. “.06” will be “6”

Instead of crating a new smarty variable in php, here is a workaround I made in the template.

{*here $dollar_per is in dollar value*}
{if $dollar_per && $dollar_per < 1}
   {if $dollar_per < 0.1} {*ex. 0.06*}
   {assign var="cent_per" value=$dollar_per|substr:2} {*get 6*}
   {assign var="cent_per" value="&nbsp;"|cat:$cent_per} {*add space before 6 in my case*}
   {else} {*ex. 0.78*}
   {assign var="cent_per" value=$dollar_per|substr:1} {*get 78*}
   {/if}
{/if}

Note: substr and cat were used!

No comments:

Post a Comment