Page 1 of 1

[SOLVED] retreive number from text input field

Posted: Fri Jul 26, 2013 3:47 am
by frankmanl
Using ListIt2 (version 1.4) I was in need of a decimal number field to add prices to my items, but no such fields are provided for. So I decided to use a text input field instead.
In order to get a correct output (decimal point and two digits) I created a UDT number_from_text that uses PHP function number_format().
The tag is {number_from_text price="1234.56"}.

Code: Select all

$price = number_format($params['price'], 2, ',', '.');
echo $price;
The output is 1.234,56*, which can be used for display at the website's front.
Of course this tag can also be used for weight or any other decimal number.
Need 3 digits in stead of 2? A decimal point instead of comma? A space instead of a point to seperate thousands? Just change the parameters of number_format().

Frank

* The output is always with 2 digits, independent of the input in the text input field. So if you have a price of say €10, it does not matter whether you enter 10 or 10,0 or 10,00 - the output will always be 10,00.
It also doesn't matter whether in the input field you use a comma, a point or the decimal point of the number keys - they will all be recognized as decimal point.

Re: retreive number from text input field

Posted: Fri Jul 26, 2013 9:10 am
by uniqu3
There is no need for a UDT, you can use number_format Smarty modifier to achieve same goal.

Code: Select all

{$foo|number_format:2:',':'.'}
or assigning

Code: Select all

{$mynumber = number_format($foo, 2, ',', '.')}
{$mynumber}