Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Number Format Modifier

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests
View previous topic :: View next topic  
Author Message
duerra
Smarty Rookie


Joined: 22 Sep 2006
Posts: 9

PostPosted: Fri Sep 22, 2006 6:51 pm    Post subject: Number Format Modifier Reply with quote

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
View user's profile Send private message
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Fri Sep 22, 2006 8:14 pm    Post subject: Reply with quote

http://smarty.php.net/manual/en/language.modifier.string.format.php
_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template
Back to top
View user's profile Send private message Visit poster's website
duerra
Smarty Rookie


Joined: 22 Sep 2006
Posts: 9

PostPosted: Mon Sep 25, 2006 1:19 pm    Post subject: Reply with quote

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
View user's profile Send private message
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Mon Sep 25, 2006 1:37 pm    Post subject: Reply with quote

http://smarty.incutio.com/?page=commify
_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon Sep 25, 2006 1:51 pm    Post subject: Reply with quote

"commify" is a great name. at first glance I thougt it may replace star spangled banners by red stars or by hammers and sickles. Wink

(SNCR)
Back to top
View user's profile Send private message Send e-mail Visit poster's website
duerra
Smarty Rookie


Joined: 22 Sep 2006
Posts: 9

PostPosted: Mon Sep 25, 2006 1:58 pm    Post subject: Reply with quote

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 Smile
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon Sep 25, 2006 2:09 pm    Post subject: Reply with quote

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. Smile
Back to top
View user's profile Send private message Send e-mail Visit poster's website
duerra
Smarty Rookie


Joined: 22 Sep 2006
Posts: 9

PostPosted: Mon Sep 25, 2006 2:18 pm    Post subject: Reply with quote

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. Smile

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. Smile
Back to top
View user's profile Send private message
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Mon Sep 25, 2006 2:48 pm    Post subject: Reply with quote

messju wrote:
"commify" is a great name. at first glance I thougt it may replace star spangled banners by red stars or by hammers and sickles. Wink

(SNCR)


LOL Laughing Ahhh mondays
_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template
Back to top
View user's profile Send private message Visit poster's website
torstein
Smarty Rookie


Joined: 10 Dec 2006
Posts: 6
Location: UK

PostPosted: Sun Dec 10, 2006 5:50 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Sun Dec 10, 2006 6:09 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
ulyxes
Smarty Rookie


Joined: 31 Jan 2007
Posts: 6

PostPosted: Fri Feb 02, 2007 5:25 pm    Post subject: Reply with quote

you can use any php function as modifier

i think

{$myvar|number_format:2:",":"."}

will work.
Back to top
View user's profile Send private message
torstein
Smarty Rookie


Joined: 10 Dec 2006
Posts: 6
Location: UK

PostPosted: Sun Feb 04, 2007 11:49 pm    Post subject: Reply with quote

Brilliant, except dec_point comes before thousands_sep:
Code:
{$myvar|number_format:2:".":","}

Thanks!! Very Happy
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP