Re: [SMARTY] money_format()

From: Date: Mon, 07 Apr 2003 15:45:40 +0000
Subject: Re: [SMARTY] money_format()
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-9170@lists.php.net to get a copy of this message
> > I'm searching for a pendant to money_format(). > > I want to modify a money string 54235.52 (coming from DB) to 54.235,52. > > > > Just write a modifier as a wrapper for the PHP function number_format: > > function smarty_modifier_number_format($string, $decimals=2, > $dec_point=".", $thousands_sep=",") > { > return number_format($string,int decimals, $decimals, $dec_point, > $thousands_sep); > } You have to be careful, that your string contains the number in a format that fits your local settings! Otherwise, "number_format" does something weird :-) Another good addition will be to check, if the given string is empty and then set it to "0" I use the following as my number formatter. It can also format money with e.g. "€" as $poststr... But it is hardcoded for two decimals (it reflects only my personal needs and is maybe not commonly usable) --> Maybe you have to combine Gabriel's and my version to a special Thorsten-Version :-) --------------------------------------------------------------- function smarty_modifier_number( $str, $poststr="", $dec_point=",", $thousands_sep="." ) { $sPost = ""; if ( isset($poststr) and !empty($poststr) ) $sPost = " " . $poststr; if ( !isset($str) or empty($str) $result = number_format( 0, 2, $dec_point, $thousands_sep ); else { $nNumber = intval( ($str * 100) + 0.5 ) /100; $result = number_format( $nNumber, 2, $dec_point, $thousands_sep ); } return( $result . $sPost ); } --------------------------------------------------------------- with regards, Matthias

« previous php.smarty.general (#9170) next »