0

私は Zend_Currency を使用しており、セントで機能するシステムの他の部分のために (ドルではなく) セントで値を保存したいと考えています。Zend_Currency オブジェクトの値をセント単位で初期化して取得したいのですが、この方法で Zend_Currency を設定する方法はありますか?

値を取得するときに 100 で割ることができることは承知していますが、国際化が必要な場合に、これがどの程度互換性があるかはわかりません。すなわち。すべての通貨は「ドル」に対して 100「セント」単位です。

4

1 に答える 1

0

すべての通貨は「ドル」に対して 100「セント」単位ですか?

いいえ。

ほとんどはそうですが、基数が「5」または基数が「1000」であるか、小数点以下がまったくないものもいくつかあります。

ソース: http://en.wikipedia.org/wiki/List_of_circulating_currencies

Zend Currency を使用して生の値を保存し、変換を行う必要があります。

    // 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';
    }
    }
}

出力: float(100.5) float(261.0387)

于 2010-12-03T16:44:46.173 に答える