2010-12-02 53 views
0

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

我知道当我检索该值时,我可以将它除以100,但我不知道这将在何时/如果我们需要国际化时如何兼容。即。是全部货币100“美分”单位到“美元”。

回答

0

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

号虽然大多数人,有几个是有“5”基地或“1000”基地或没有小数的。

来源:http://en.wikipedia.org/wiki/List_of_circulating_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'; 
    } 
    } 
} 

输出: float(100.5) float(261.0387)