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

modifier.money_format.php

 
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 -> Plugins
View previous topic :: View next topic  
Author Message
desean84
Smarty Rookie


Joined: 04 Aug 2003
Posts: 7

PostPosted: Sun Aug 17, 2003 6:55 am    Post subject: modifier.money_format.php Reply with quote

found this money formatting plugin on the smarty-general mailing list. hope Gabriel Birke wouldn't mind me taking his original idea and post it here.
[php:1:882f07498e]<?php

/*
* Smarty plugin
* -------------------------------------------------------------
* Type: modifier
* Name: money_format
* File: modifier.money_format.php
* Purpose: format currency amount
* Input: string: money value
* decimals: number of decimal places
* dec_point: string for decimal
* thousands_sep: string for thousands separation
* Example: {$value|money_format:2:".":","}
* Author: Gabriel Birke <birke {at} kontor4.de>
* Modfied By: Desean [http://www.eighteencharacters.com]
* Modification: Check if string is numeric first
* Source URL: http://marc.theaimsgroup.com/?l=smarty-general&m=104972875929464&w=2
* Date: 2003-04-07 15:19:14
* Modfied on: 16 Aug 2003
*/

function smarty_modifier_money_format($string, $decimals=2, $dec_point=".", $thousands_sep=",")
{
if (is_numeric($string)) // check if it's a number
{
return number_format($string, $decimals, $dec_point, $thousands_sep);
}
else
{
return $string;
}
}

/* vim: set expandtab: */

?>[/php:1:882f07498e]
_________________
Dragons Online!
Back to top
View user's profile Send private message
sagi
Smarty Regular


Joined: 30 Jul 2003
Posts: 43

PostPosted: Wed Nov 12, 2003 2:06 pm    Post subject: Reply with quote

But why? there is a PHP function named money_format() that can do that.

I already added a wrapper modifier to the wiki few weeks ago:
http://smarty.incutio.com/?page=MoneyFormatPlugin
Back to top
View user's profile Send private message
mitchenall
Smarty Pro


Joined: 27 Feb 2004
Posts: 107
Location: London, UK

PostPosted: Fri Feb 27, 2004 3:08 am    Post subject: Re: modifier.money_format.php Reply with quote

How about a function which does the conversion using the PEAR Services_ExchangeRates package? We only added special formatting for GBP, USD and Euros, but this could easily be added to. This was just a simple experiment, and we're not sure whether we're going to bother using it yet.

Code:
<?php

/**
 *   SMARTY FUNCTION - money
 *   =====================================================
 *
 *   This plug-in takes a numeric amount and converts it to
 *   another currency or simply formats it if you don't pass
 *  a 'to' currency.  The default currency is GBP, so if
 *   the 'from' param is missing, this currency will be assumed.
 *
 *   The conversion is performed using the PEAR::Services_ExchangeRates
 *   package using the default service for the European Central Bank.
 *   If no 'to' is passed, the function does no conversion, but still
 *  formats the amount.
 *
 *   Example Usage:
 *
 *   {money amount=25.99} displays the amount in GBP
 *  {money amount=25.99 to=EUR} converts the amount to Euros
 *                        and displays it
 *
 *   @author Mark Mitchenall <mark@standingwave.co.uk>
 *   @copyright Standingwave Ltd, 2004
 *
 **/

require_once 'Services/ExchangeRates.php';

function smarty_function_money ($params, &$smarty)
{
   $fromCurrency = isset($params['from'])       ? $params['from']    : 'GBP' ;
   $toCurrency   = isset($params['to'])         ? $params['to']      : $fromCurrency ;
   $amount        = isset($params['amount'])      ? $params['amount'] : 1 ;

   $service        = isset($params['service'])   ? $params['service'] : 'ECB' ;
   
   $options = array( 'roundToDecimal' => 2 ) ;
   
   if($fromCurrency != $toCurrency) {   
      $conv = new Services_ExchangeRates($service, 'UN', 'UN', $options) ;
      $amount = $conv->convert($fromCurrency, $toCurrency, $amount) ;
   }
   
   switch ($toCurrency) {
   
      case 'GBP' :
         $output = '&&'.$amount ;   
         break ;

      case 'USD' :
         $output = '$&'.$amount.'&US' ;   
         break ;

      case 'EUR' :
         $output = '&&'.$amount ;   
         break ;

      default :
         $output = $amount.'&'.$toCurrency ;   
         break ;
   }
   
   echo $output ;
   
}

?>
Back to top
View user's profile Send private message Visit poster's website
philjohn
Smarty Rookie


Joined: 26 Feb 2004
Posts: 8

PostPosted: Fri Feb 27, 2004 11:55 pm    Post subject: Reply with quote

sagi wrote:
But why? there is a PHP function named money_format() that can do that.

I already added a wrapper modifier to the wiki few weeks ago:
http://smarty.incutio.com/?page=MoneyFormatPlugin


I probably guess it's because the money_format function was only added in PHP 4.3.0. Loads and loads of lame hosting companies are still subjecting their customers to versions as old as 4.0.x and 4.1.x so therefore some users will find this interesting.
Back to top
View user's profile Send private message
chidera
Smarty Rookie


Joined: 08 Dec 2004
Posts: 22

PostPosted: Wed Dec 08, 2004 10:08 pm    Post subject: Reply with quote

sagi wrote:
But why? there is a PHP function named money_format() that can do that.

I already added a wrapper modifier to the wiki few weeks ago:
http://smarty.incutio.com/?page=MoneyFormatPlugin


If you want to output the number and allow the template to determine how it looks, then the PHP functions aren't very useful.

IMHO, of course. Wink
Back to top
View user's profile Send private message
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 -> Plugins 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