zend货币负号

时间:2011-12-05 11:34:44

标签: zend-framework zend-view

您好我正在使用 Zend_currency

class Currency extends Zend_View_Helper_Abstract
{

    public function currency($number, $locale = 'it_IT') {
        $currency = new Zend_Currency($locale);

        $number = $number + 0.00;//convert to float

        return $currency->toCurrency((float) $number);

    }
}

在一些视图中.phtml文件

echo $this->currency($gimme_my_money);

这就是我得到的

€ 19.373,25
-€ 116,07

我怎样才能打印负数,如

€ -116,07

3 个答案:

答案 0 :(得分:5)

只需覆盖格式选项,如下所示:

$cur = new Zend_Currency(array('format' => '¤ #,##0.00;¤ -#,##0.00'));

诀窍在字符串的第二部分(逗号之后),我已经检查了意大利语语言环境,并且提供的格式字符串是¤#,## 0.00。

使用ZF 1.11.7进行测试

答案 1 :(得分:1)

我不认为这种格式化选项是内置于Zend_Currency中的。

您可以做的是将货币符号移到右侧:

$this->view->total = new Zend_Currency(array('value' => $total, 'position' => Zend_Currency::RIGHT));

然后您的货币将显示右侧的货币符号:

 -19.373,25 €

如果您想要自定义格式,并在符号后面添加负号(€ -116,07),则必须编写自己的货币格式化程序或构建在Zend_Currency

之上

答案 2 :(得分:0)

试试这个:

class My_View_Helper_Currency extends Zend_View_Helper_Abstract
{
    /**
     * Format a numeric currency value and return it as a string
     *
     * @param int|float $value   any value that return true with is_numeric
     * @param array     $options additional options to pass to the currency
     *                           constructor
     * @param string    $locale  locale value
     *
     * @throws InvalidParameterException if the $value parameter is not numeric
     * @return string the formatted value
     */
    public function currency($value, $options = array(), $locale = null)
    {
        if (!is_numeric($value)) {
            throw new InvalidArgumentException(
                'Numeric argument expected ' . gettype($value) . ' given'
            );
        }
        $options = array_merge($options, array('value' => $value));
        $currency = new Zend_Currency($options, $locale);
        return $currency->toString();
    }
}
相关问题