如何使用货币
在自己的程序中使用 Example #1 从实际地方创建 Zend_Currency 的实例
期望通过用户(的输入)或环境,你有 'en_US' 设置作为实际地方,通过使用不带参数创建
$currency = new Zend_Currency();
从 Zend Framework 1.7.0 开始,
// in your bootstrap file
$locale = new Zend_Locale('de_AT');
Zend_Registry::set('Zend_Locale', $locale);
// somewhere in your application
$currency = new Zend_Currency();
当然,根据需要,若干参数可以在创建时给出,每个参数都是可选的和被禁止,甚至参数的顺序也可以交换。每个参数的意思描述如下:
Example #2 创建 Zend_Currency 实例的其它例子
// expect standard locale 'de_AT'
// creates an instance from 'en_US' using 'USD' which is default
// currency for 'en_US'
$currency = new Zend_Currency('en_US');
// creates an instance from the actual locale ('de_AT') using 'EUR' as
// currency
$currency = new Zend_Currency();
// creates an instance using 'EUR' as currency, 'en_US' for number
// formating
$currency = new Zend_Currency('en_US', 'EUR');
如果想使用缺省值,你可以禁止任何参数,在处理货币方面没有副作用,例如这在当你不知道某地区的缺省货币很有用。
从货币创建输出可以用方法 toCurrency() 把存在的数值转换成格式化的货币输出,它带有一个可以被转换的数值,这个数值可以是任何标准化的数字。
如果有个需要转换的本地化的数字,首先用 Zend_Locale_Format::getNumber() 来标准化,然后用
Example #3 为货币创建输出
// creates an instance with 'en_US' using 'USD' which is the default
// values for 'en_US'
$currency = new Zend_Currency('en_US');
// prints '$ 1,000.00'
echo $currency->toCurrency(1000);
// prints '$ 1.000,00'
echo $currency->toCurrency(1000, array('format' => 'de_AT'));
// prints '$ ١٬٠٠٠٫٠٠'
echo $currency->toCurrency(1000, array('script' => 'Arab'));
修改货币格式
用来创建 货币输出的格式包括下面部分:
如果确实需要修改格式,你可以用 setFormat() 方法。它带有一个数组,包括所有你向修改的选项。
Example #4 修改货币的显示格式
// creates an instance with 'en_US' using 'USD', 'Latin' and 'en_US' as
// these are the default values from 'en_US'
$currency = new Zend_Currency('en_US');
// prints 'US$ 1,000.00'
echo $currency->toCurrency(1000);
$currency->setFormat('display' => Zend_Currency::USE_NAME,
'position' => Zend_Currency::RIGHT);
// prints '1.000,00 US Dollar'
echo $currency->toCurrency(1000);
$currency->setFormat('name' => 'American Dollar');
// prints '1.000,00 American Dollar'
echo $currency->toCurrency(1000);
Zend_Currency 的信息方法
当然,
函数 Example #5 从货币中获取信息
// creates an instance with 'en_US' using 'USD', 'Latin' and 'en_US'
// as these are the default values from 'en_US'
$currency = new Zend_Currency('en_US');
// prints '$'
echo $currency->getSymbol();
// prints 'EUR'
echo $currency->getShortName('EUR');
// prints 'Österreichische Schilling'
echo $currency->getName('ATS', 'de_AT');
// returns an array with all regions where USD is used
print_r($currency->getRegionList();
// returns an array with all currencies which were ever used in this
// region
print_r($currency->getCurrencyList('de_AT');
设置新缺省值
Example #6 设置新地方
// 获得 US 货币
$currency = new Zend_Currency('en_US');
print $currency->toCurrency(1000);
// 获得 AT 货币
$currency->setLocale('de_AT');
print $currency->toCurrency(1000);
加速 Zend_Currency
通过 Example #7 缓存货币
// 创建一个缓存对象
$cache = Zend_Cache::factory('Core',
'File',
array('lifetime' => 120,
'automatic_serialization' => true),
array('cache_dir'
=> dirname(__FILE__) . '/_files/'));
Zend_Currency::setCache($cache);
|
|