Zend_Currency 值以美分代替美元

发布于 2024-10-05 04:59:15 字数 214 浏览 7 评论 0原文

我正在使用 Zend_Currency 并希望以美分存储值(而不是美元),因为系统的其他部分以美分工作。所以,我想初始化和检索 Zend_Currency 对象的值(以美分为单位),有没有办法以这种方式配置 Zend_Currency ?

我知道当我检索该值时我可以除以 100,但我不知道当/如果我们需要国际化时这会有多兼容。 IE。 所有货币都是100“分”单位兑“美元”吗?

I am using Zend_Currency and would like to store the values in cents as (opposed to to dollars) due to other parts of the system that work in cents. So, I would like to initialise and retrieve values for Zend_Currency objects in cents, is there a way to configure Zend_Currency this way?

I am aware I could just divide by 100 when I retrieve the value, but I don't know how compatible this would be when/if we need to internationalize. ie. Are all currencies 100 "cent" units to the "dollar".

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

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

发布评论

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

评论(1

这样的小城市 2024-10-12 04:59:15

所有货币的单位都是“美元”100“分”吗?

不是。

虽然大多数都是这样,但也有少数货币的基数为“5”或“1000”,或者没有小数点全部。

来源:http://en.wikipedia.org/wiki/List_of_circulate_currencies

您应该使用 Zend 货币存储原始值并让它为您进行转换。

    // original money
    $money = new Zend_Currency('US');
    $money->setValue('100.50');

    // conversion
    $oman = new Zend_Currency('OM');
    $oman->setService(new My_Currency_Exchange());
    $oman->setValue($money->getValue(), $money->getShortName());
    var_dump($money->getValue(), $oman->getValue());

注意:My_Currency_Exchange() 是我创建的一个虚拟类,它看起来像这样:

<?php

class My_Currency_Exchange implements Zend_Currency_CurrencyInterface
{
public function getRate($from, $to)
{
    if ($from !== "USD") {
        throw new Exception ('We only do USD : '  . $from);
    }

    switch ($to) {
        case 'OMR':
            // value from xe.com as of today
            return '2.59740';
    }
    }
}

输出:
浮点(100.5)浮点(261.0387)

Are all currencies 100 "cent" units to the "dollar"?

No.

Although most do, there are a few that have a base of '5' or a base of '1000' or no decimal at all.

source: http://en.wikipedia.org/wiki/List_of_circulating_currencies

You should be using Zend Currency to store the raw value and let it do conversions for you.

    // original money
    $money = new Zend_Currency('US');
    $money->setValue('100.50');

    // conversion
    $oman = new Zend_Currency('OM');
    $oman->setService(new My_Currency_Exchange());
    $oman->setValue($money->getValue(), $money->getShortName());
    var_dump($money->getValue(), $oman->getValue());

Note: My_Currency_Exchange() is a dummy class I've created, it looks like this:

<?php

class My_Currency_Exchange implements Zend_Currency_CurrencyInterface
{
public function getRate($from, $to)
{
    if ($from !== "USD") {
        throw new Exception ('We only do USD : '  . $from);
    }

    switch ($to) {
        case 'OMR':
            // value from xe.com as of today
            return '2.59740';
    }
    }
}

output:
float(100.5) float(261.0387)

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