root

Changeset 849

Show
Ignore:
Timestamp:
06/09/10 13:28:56 (3 months ago)
Author:
wbond
Message:

Added the $remove_zero_fraction parameter to fMoney::format() and fORMMoney::prepareMoneyColumn()

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • fMoney.php

    r577 r849 Hide Line Numbers
    33 * Represents a monetary value - USD are supported by default and others can be added via ::defineCurrency() 
    44 *  
    5  * @copyright  Copyright (c) 2008-2009 Will Bond 
     5 * @copyright  Copyright (c) 2008-2010 Will Bond 
    66 * @author     Will Bond [wb] <will@flourishlib.com> 
    77 * @license    http://flourishlib.com/license 
     
    1010 * @link       http://flourishlib.com/fMoney 
    1111 *  
    12  * @version    1.0.0b2 
     12 * @version    1.0.0b3 
     13 * @changes    1.0.0b3  Added the `$remove_zero_fraction` parameter to ::format() [wb, 2010-06-09] 
    1314 * @changes    1.0.0b2  Fixed a bug with calling ::format() when a format callback is set, fixed `NULL` `$element` handling in ::getCurrencyInfo() [wb, 2009-03-24] 
    1415 * @changes    1.0.0b   The initial implementation [wb, 2008-08-10] 
     
    142143     * Allows setting a callback to translate or modify any return values from ::format() 
    143144     *  
    144      * @param  callback $callback  The callback to pass all fNumber objects to. Should accept an fNumber object and a string currency abbreviation and return a formatted string. 
     145     * @param  callback $callback  The callback to pass all fNumber objects to. Should accept an fNumber object, a string currency abbreviation and a boolean indicating if a zero-fraction should be removed - it should return a formatted string. 
    145146     * @return void 
    146147     */ 
     
    431432     * Formats the amount by preceeding the amount with the currency symbol and adding thousands separators 
    432433     *  
     434     * @param  boolean $remove_zero_fraction  If `TRUE` and all digits after the decimal place are `0`, the decimal place and all zeros are removed 
    433435     * @return string  The formatted (and possibly converted) value 
    434436     */ 
    435     public function format(
     437    public function format($remove_zero_fraction=FALSE
    436438    { 
    437439        if (self::$format_callback !== NULL) { 
    438             return call_user_func(self::$format_callback, $this->amount, $this->currency); 
     440            return call_user_func(self::$format_callback, $this->amount, $this->currency, $remove_zero_fraction); 
    439441        } 
    440442         
     
    462464        $integer  = join(',', $int_sections); 
    463465        $fraction = (strlen($fraction)) ? '.' . $fraction : ''; 
     466         
     467        if ($remove_zero_fraction && rtrim($fraction, '.0') === '') { 
     468            $fraction = ''; 
     469        } 
    464470         
    465471        return $sign . $symbol . $integer . $fraction; 
     
    623629 
    624630/** 
    625  * Copyright (c) 2008-2009 Will Bond <will@flourishlib.com> 
     631 * Copyright (c) 2008-2010 Will Bond <will@flourishlib.com> 
    626632 *  
    627633 * Permission is hereby granted, free of charge, to any person obtaining a copy 
  • fORMMoney.php

    r833 r849 Hide Line Numbers
    1111 * @link       http://flourishlib.com/fORMMoney 
    1212 *  
    13  * @version    1.0.0b8 
     13 * @version    1.0.0b9 
     14 * @changes    1.0.0b9  Added the `$remove_zero_fraction` parameter to prepare methods [wb, 2010-06-09] 
    1415 * @changes    1.0.0b8  Changed validation messages array to use column name keys [wb, 2010-05-26] 
    1516 * @changes    1.0.0b7  Fixed the `set` methods to return the record object in order to be consistent with all other `set` methods [wb, 2010-03-15] 
     
    342343        $value = $values[$column]; 
    343344         
     345        $remove_zero_fraction = FALSE; 
     346        if (count($parameters)) { 
     347            $remove_zero_fraction = $parameters[0]; 
     348        } 
     349         
    344350        if ($value instanceof fMoney) { 
    345             $value = $value->format(); 
     351            $value = $value->format($remove_zero_fraction); 
    346352        } 
    347353         
     
    422428                $signature .= " * resulting in the value including the currency symbol and thousands separators\n"; 
    423429                $signature .= " * \n"; 
     430                $signature .= " * @param  boolean \$remove_zero_fraction  If a fraction of all zeros should be removed\n"; 
    424431                $signature .= " * @return string  The HTML-ready value\n"; 
    425432                $signature .= " */\n"; 
    426433            } 
    427434            $prepare_method = 'prepare' . $camelized_column; 
    428             $signature .= 'public function ' . $prepare_method . '()'; 
     435            $signature .= 'public function ' . $prepare_method . '($remove_zero_fraction=FALSE)'; 
    429436             
    430437            $signatures[$prepare_method] = $signature;