本文目录导读:

对于PHP项目中处理乌兹别克斯坦苏姆(UZS) 与 eUZS(电子苏姆) 的数字问题,需要重点关注以下几个方面:
数值精度处理
乌兹别克斯坦苏姆面值较大(1美元≈12,500 UZS),且涉及电子货币,建议:
// 使用BCMath或GMP扩展处理高精度计算
// composer.json 确保启用 bcmath 扩展
/**
* 安全处理UZS金额
*/
class UZSHandler {
/**
* 格式化为标准UZS显示
*/
public static function format(float $amount, string $currency = 'UZS'): string {
$formatter = new NumberFormatter('uz_UZ', NumberFormatter::CURRENCY);
return $formatter->formatCurrency($amount, $currency);
}
/**
* 高精度加法
*/
public static function add(string $a, string $b, int $scale = 2): string {
return bcadd($a, $b, $scale);
}
/**
* 转换为eUZS(1 eUZS = 1 UZS)
*/
public static function toEUZS(string $uzs_amount): string {
// eUZS是UZS的电子形式,1:1兑换
return bcadd($uzs_amount, '0', 2);
}
}
数据库存储设计
-- 推荐使用DECIMAL精确类型
CREATE TABLE transactions (
id INT AUTO_INCREMENT PRIMARY KEY,
amount_uzs DECIMAL(18, 2) NOT NULL DEFAULT 0.00,
amount_euzs DECIMAL(18, 2) NOT NULL DEFAULT 0.00,
currency_type ENUM('UZS', 'eUZS') NOT NULL,
exchange_rate DECIMAL(12, 4) DEFAULT 1.0000,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
汇率处理(UZS ↔ eUZS)
class CurrencyExchangeService {
const UZS_TO_EUZS = 1.0; // 固定1:1
const EUZS_TO_UZS = 1.0;
/**
* 转换金额
*/
public function convert(string $amount, string $from, string $to): string {
if ($from === $to) return $amount;
if ($from === 'UZS' && $to === 'eUZS') {
return bcmul($amount, (string)self::UZS_TO_EUZS, 2);
}
if ($from === 'eUZS' && $to === 'UZS') {
return bcmul($amount, (string)self::EUZS_TO_UZS, 2);
}
throw new InvalidArgumentException('不支持的货币转换');
}
/**
* 获取实时汇率(实际项目中可从API获取)
*/
public function getLiveRate(): array {
return [
'UZS_to_eUZS' => 1.0000,
'eUZS_to_UZS' => 1.0000,
'last_updated' => date('Y-m-d H:i:s')
];
}
}
前端显示格式化
// Vue.js 示例
const formatUZS = (amount) => {
return new Intl.NumberFormat('uz-UZ', {
style: 'currency',
currency: 'UZS',
minimumFractionDigits: 2,
maximumFractionDigits: 2
}).format(amount);
};
// 显示 eUZS 特殊标记
const formatEUZS = (amount) => {
return `${formatUZS(amount)} eUZS`;
};
完整示例:交易处理
class TransactionProcessor {
public function processPayment(float $amount, string $currency): array {
// 使用BCMath确保精度
$amount_str = number_format($amount, 2, '.', '');
if ($currency === 'UZS') {
$uzs = $amount_str;
$euzs = self::toEUZS($uzs);
} elseif ($currency === 'eUZS') {
$euzs = $amount_str;
$uzs = self::toUZS($euzs);
} else {
throw new InvalidArgumentException('不支持的货币类型');
}
return [
'amount_uzs' => $uzs,
'amount_euzs' => $euzs,
'exchange_rate' => '1.0000',
'timestamp' => date('Y-m-d H:i:s')
];
}
}
注意事项
- 精度问题:UZS面值大,避免使用float/double
- 存储单位:建议数据库使用DECIMAL(18,2)
- 显示格式化:UZS通常显示为 "10 000,00" 格式
- eUZS特殊性:电子货币可能涉及智能合约或区块链交互
- 合规要求:需遵守乌兹别克斯坦央行关于电子货币的规定
常用工具库
# 推荐安装的PHP扩展
composer require moneyphp/money
composer require brick/money
# 示例使用
use Money\Money;
use Money\Currency;
$uzs = new Money(1000000, new Currency('UZS')); // 10,000.00 UZS
$euzs = new Money(1000000, new Currency('eUZS')); // 10,000.00 eUZS
这样的实现可以确保UZS和eUZS在PHP项目中安全、精确地处理。