[SOLVED] retreive number from text input field

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
User avatar
frankmanl
Power Poster
Power Poster
Posts: 425
Joined: Sat Jul 12, 2008 3:50 am

[SOLVED] retreive number from text input field

Post 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.
Last edited by frankmanl on Sun Nov 10, 2013 3:41 am, edited 1 time in total.
uniqu3

Re: retreive number from text input field

Post 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}
Post Reply

Return to “Tips and Tricks”