Prado\I18N\core\NumberFormat::format PHP Method

format() public method

For the number for a certain pattern. The valid patterns are 'c', 'd', 'e', 'p' or a custom pattern, such as "#.000" for 3 decimal places.
public format ( $number, $pattern = 'd', $currency = 'USD', $charset = 'UTF-8' ) : string
return string formatted number string
    function format($number, $pattern = 'd', $currency = 'USD', $charset = 'UTF-8')
    {
        $oldLocale = setLocale(LC_NUMERIC, '0');
        setlocale(LC_NUMERIC, 'C');
        $this->setPattern($pattern);
        if (strtolower($pattern) == 'p') {
            $number = $number * 100;
        }
        $string = (string) $number;
        $decimal = $this->formatDecimal($string);
        $integer = $this->formatInteger(abs($number));
        if (strlen($decimal) > 0) {
            $result = $integer . $decimal;
        } else {
            $result = $integer;
        }
        //get the suffix
        if ($number >= 0) {
            $suffix = $this->formatInfo->PositivePattern;
        } else {
            if ($number < 0) {
                $suffix = $this->formatInfo->NegativePattern;
            } else {
                $suffix = array("", "");
            }
        }
        //append and prepend suffix
        $result = $suffix[0] . $result . $suffix[1];
        //replace currency sign
        $symbol = @$this->formatInfo->getCurrencySymbol($currency);
        if ($symbol === null) {
            $symbol = $currency;
        }
        $result = str_replace('¤', $symbol, $result);
        setlocale(LC_NUMERIC, $oldLocale);
        return I18N_toEncoding($result, $charset);
    }

Usage Example

コード例 #1
0
ファイル: NumberFormatTest.php プロジェクト: pradosoft/prado
 function testLocalizedCurrencyFormats2()
 {
     $it = new NumberFormat('it_IT');
     $number = 12.41;
     $wanted = '12,41';
     $this->assertEquals($wanted, $it->format($number, 'd'));
     $number = 10.23;
     $wanted = '10,23';
     $this->assertEquals($wanted, $it->format($number, 'd'));
     $number = 10010.23;
     $wanted = '10.010,23';
     $this->assertEquals($wanted, $it->format($number, 'd'));
     $old = setlocale(LC_ALL, "0");
     setlocale(LC_ALL, "it_IT");
     $number = 12.41;
     $wanted = '12,41';
     $this->assertEquals($wanted, $it->format($number, 'd'));
     $number = 10.23;
     $wanted = '10,23';
     $this->assertEquals($wanted, $it->format($number, 'd'));
     $number = 10010.23;
     $wanted = '10.010,23';
     $this->assertEquals($wanted, $it->format($number, 'd'));
     setlocale(LC_ALL, $old);
 }
All Usage Examples Of Prado\I18N\core\NumberFormat::format