zend 货币负号

发布于 2024-12-19 14:00:27 字数 566 浏览 0 评论 0原文

你好,我

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);

使用 Zend_currency ,这就是我得到的

€ 19.373,25
-€ 116,07

,如何让它打印负数,例如

€ -116,07

Hello i am using 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);

    }
}

in a some view .phtml file

echo $this->currency($gimme_my_money);

and this is what i get

€ 19.373,25
-€ 116,07

how can i get it to print negative numbers like

€ -116,07

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴情 2024-12-26 14:00:27

只需像这样覆盖格式选项:

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

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

这是用 ZF 1.11.7 测试的

Just overwrite the format option like this:

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

The trick is in the second part of the string (after the comma), I've checked it for Italian locale and the format string provided there is ¤ #,##0.00.

This is tested with ZF 1.11.7

万水千山粽是情ミ 2024-12-26 14:00:27

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

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

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

然后您的货币将显示在右侧,货币符号如下:

 -19.373,25 €

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

I don't think this formatting option is built into Zend_Currency.

What you can do, is move the currency symbol to the right side:

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

And then your currencies will be displayed with the currency symbol on the right:

 -19.373,25 €

If you want a custom formatting, with the negative sign after the symbol, (€ -116,07), you will have to write your own currency formatter or built on top of Zend_Currency

杯别 2024-12-26 14:00:27

试试这个:

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();
    }
}

try this:

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文