View previous topic :: View next topic |
Author |
Message |
duerra Smarty Rookie
Joined: 22 Sep 2006 Posts: 9
|
Posted: Fri Sep 22, 2006 6:51 pm Post subject: Number Format Modifier |
|
|
I'm extremely surprised that this isnt' built into Smarty already, and I have built my own version for the time being - but I think that smarty should implement a standard number format modifier for variables. |
|
Back to top |
|
TGKnIght Smarty Junkie
Joined: 07 Sep 2005 Posts: 580 Location: Philadelphia, PA
|
|
Back to top |
|
duerra Smarty Rookie
Joined: 22 Sep 2006 Posts: 9
|
Posted: Mon Sep 25, 2006 1:19 pm Post subject: |
|
|
TGKnIght wrote: | http://smarty.php.net/manual/en/language.modifier.string.format.php |
This is not the same as the number format function in PHP, which adds a thousands delimiter (my main reason for requesting this).
Since Smarty is for templating and page formatting, it would seem like common sense to have a such a function implemented into the core.
Last edited by duerra on Mon Sep 25, 2006 1:55 pm; edited 1 time in total |
|
Back to top |
|
TGKnIght Smarty Junkie
Joined: 07 Sep 2005 Posts: 580 Location: Philadelphia, PA
|
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Mon Sep 25, 2006 1:51 pm Post subject: |
|
|
"commify" is a great name. at first glance I thougt it may replace star spangled banners by red stars or by hammers and sickles.
(SNCR) |
|
Back to top |
|
duerra Smarty Rookie
Joined: 22 Sep 2006 Posts: 9
|
Posted: Mon Sep 25, 2006 1:58 pm Post subject: |
|
|
TGKnIght wrote: | http://smarty.incutio.com/?page=commify |
As noted, I already implemented my own version, just that I think this should be built into the core. And actually, there's no need to create such a complex function manually - just mapping a modifier handler to PHP's number_format() function works just fine.
I appreciate the discussion and consideration, though |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Mon Sep 25, 2006 2:09 pm Post subject: |
|
|
duerra wrote: | just mapping a modifier handler to PHP's number_format() function works just fine. |
just reading the manual on modifiers and noting that you can use php's number_format function as a modifier directly works also. |
|
Back to top |
|
duerra Smarty Rookie
Joined: 22 Sep 2006 Posts: 9
|
Posted: Mon Sep 25, 2006 2:18 pm Post subject: |
|
|
messju wrote: | duerra wrote: | just mapping a modifier handler to PHP's number_format() function works just fine. |
just reading the manual on modifiers and noting that you can use php's number_format function as a modifier directly works also. |
Si, si.
Anyway, consider it a suggestion. Just thinkin' of other users, that's all. There's more than one way to skin a cat, but some ways may take more work than others. |
|
Back to top |
|
TGKnIght Smarty Junkie
Joined: 07 Sep 2005 Posts: 580 Location: Philadelphia, PA
|
|
Back to top |
|
torstein Smarty Rookie
Joined: 10 Dec 2006 Posts: 6 Location: UK
|
Posted: Sun Dec 10, 2006 5:50 pm Post subject: |
|
|
Is the conclusion that there is no way to write a set of 'sprintf' / 'text_format' arguments that will add thousand separators (i.e. comma) ?
The 'commify' modifier is useful enough, but not if using 'math' in the template - so a 'text_format' solution would be best (if possible).
Any suggestions?
Also, what's the difference between:
Code: | string_format:"%01.2f" |
and Code: | string_format:"%.2f" | ?
Both seem to output the same result. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Sun Dec 10, 2006 6:09 pm Post subject: |
|
|
torstein wrote: | Is the conclusion that there is no way to write a set of 'sprintf' / 'text_format' arguments that will add thousand separators (i.e. comma) ?
|
correct. sprintf() is borrowed from C where the standard does not consider any seperator for thousands in sprintf().
Quote: | The 'commify' modifier is useful enough, but not if using 'math' in the template - so a 'text_format' solution would be best (if possible). |
assign numeric numbers to the templates (integers or doubles), and use number_format() as a modifier to format the output. |
|
Back to top |
|
ulyxes Smarty Rookie
Joined: 31 Jan 2007 Posts: 6
|
Posted: Fri Feb 02, 2007 5:25 pm Post subject: |
|
|
you can use any php function as modifier
i think
{$myvar|number_format:2:",":"."}
will work. |
|
Back to top |
|
torstein Smarty Rookie
Joined: 10 Dec 2006 Posts: 6 Location: UK
|
Posted: Sun Feb 04, 2007 11:49 pm Post subject: |
|
|
Brilliant, except dec_point comes before thousands_sep:
Code: | {$myvar|number_format:2:".":","} |
Thanks!! |
|
Back to top |
|
|