11

As i am new to smarty, I am not at able to convert floating number to int. Ex: 12.234 => 12 please help me if u find any solution

sandeep
  • 2,862
  • 9
  • 44
  • 54

4 Answers4

15

Why don't you cast it before attaching it to the view. There is no reason to pass the view data that needs to be further processed.

$int = (int) $float;

$smarty->assign(array(
   'number' = $int
));

If you really must get the integer portion of a float using Smarty, try this...

{$number|string_format:"%d"}

That is like PHP's printf().

alex
  • 479,566
  • 201
  • 878
  • 984
  • I need to convert in .tpl(template)? – sandeep Mar 09 '11 at 07:17
  • @sandeep: No, this is *before* you assign the variable itself; in your `.php` file. – Russell Dias Mar 09 '11 at 07:18
  • ya i got it. But i need to change in tpl file.I don have access to .php file. – sandeep Mar 09 '11 at 07:20
  • There is a perfect reason to pass data that needs to be processed: If you work on a modularized system (e.g. a blog or shop), you might want to get a small change like this done in a template change, without writing a module, overriding the controller and caring about future updates. Anyway, +1 for naming both solutions. :) – Zsolt Szilagyi Jan 25 '16 at 19:56
5

Also you should be able doing this: {$variable|intval}

Luca Reghellin
  • 7,426
  • 12
  • 73
  • 118
1

this might work give a try

(string)((int)$float)

that too check this link

http://www.smarty.net/forums/viewtopic.php?p=61912

Harish
  • 2,311
  • 4
  • 23
  • 28
0

This can be done:

{$converted = settype($myVar, 'integer')}

You'll need to assign the return value of settype, bool, else the value will be displayed in the UI.

Darin Peterson
  • 1,262
  • 2
  • 15
  • 25