本文目录导读:

在PHP项目中处理“数字加纳塞地(GHS)”和“电子加纳塞地(eGHS)”涉及货币单位、格式化和可能的汇率转换,以下是关键实现方法:
基本货币格式显示
class GhanaCediFormatter {
// 格式化为加纳塞地显示
public static function formatGHS($amount) {
return 'GH¢ ' . number_format($amount, 2);
}
// 格式化 eGHS(通常同值但可标记)
public static function formatEGHS($amount) {
return 'eGH¢ ' . number_format($amount, 2);
}
}
// 使用示例
echo GhanaCediFormatter::formatGHS(1500.50); // GH¢ 1,500.50
echo GhanaCediFormatter::formatEGHS(500.00); // eGH¢ 500.00
数字与货币转换(金额处理)
class GhanaCediConverter {
// 数字转加纳塞地字符串
public static function numberToGHS($number) {
$formatted = number_format($number, 2, '.', ',');
return "GH¢ {$formatted}";
}
// 解析用户输入的金额(处理前缀/后缀)
public static function parseAmount($input) {
// 移除非数字字符(保留小数点和负号)
$cleaned = preg_replace('/[^0-9.-]/', '', $input);
return floatval($cleaned);
}
// 金额转分(最小单位)
public static function toPesewas($amount) {
return intval(round($amount * 100));
}
// 从分转回塞地
public static function fromPesewas($pesewas) {
return $pesewas / 100;
}
}
汇率处理(GHS ↔ eGHS)
class EGHSCurrency {
private $exchangeRate; // 1 GHS = ? eGHS
public function __construct($rate = 1.0) {
$this->exchangeRate = $rate;
}
// GHS 转 eGHS
public function convertToEGHS($ghsAmount) {
return $ghsAmount * $this->exchangeRate;
}
// eGHS 转 GHS
public function convertToGHS($eghsAmount) {
return $eghsAmount / $this->exchangeRate;
}
// 获取最新汇率(示例API)
public function fetchLiveRate() {
// 实际项目中调用银行或央行API
// $response = file_get_contents('https://api.bog.gov.gh/rates');
// return json_decode($response)->rate;
return 1.0; // 当前假设1:1
}
}
货币检测与统一处理
class CurrencyDetector {
public static function detectAndConvert($input) {
// 检测字符串中的货币类型
if (preg_match('/^eGH[¢ C]/i', $input)) {
$amount = GhanaCediConverter::parseAmount($input);
return ['type' => 'eGHS', 'amount' => $amount];
} else {
$amount = GhanaCediConverter::parseAmount($input);
return ['type' => 'GHS', 'amount' => $amount];
}
}
// 统一转换为GHS(用于计算)
public static function normalizeToGHS($input, $eghsRate = 1.0) {
$detected = self::detectAndConvert($input);
if ($detected['type'] === 'eGHS') {
return $detected['amount'] / $eghsRate;
}
return $detected['amount'];
}
}
数据库存储建议
// 表结构建议
CREATE TABLE transactions (
id INT AUTO_INCREMENT PRIMARY KEY,
amount_ghs DECIMAL(15,2) NOT NULL, -- 统一以GHS存储
amount_eghs DECIMAL(15,2) DEFAULT NULL,
currency_type ENUM('GHS', 'eGHS') DEFAULT 'GHS',
rate_applied DECIMAL(10,6) DEFAULT 1.000000,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
// PHP插入示例
$stmt = $pdo->prepare("INSERT INTO transactions
(amount_ghs, amount_eghs, currency_type, rate_applied)
VALUES (?, ?, ?, ?)");
// 如果用户输入的是eGHS
$eghsAmount = 200.00;
$rate = 1.05; // 假设汇率
$ghsAmount = $eghsAmount / $rate;
$stmt->execute([$ghsAmount, $eghsAmount, 'eGHS', $rate]);
货币数字转大写(加纳塞地专用)
class GhanaCediToWords {
public static function convert($number) {
$pesewas = round(($number - floor($number)) * 100);
$cedis = floor($number);
$words = self::numberToWords($cedis) . " Ghana Cedis";
if ($pesewas > 0) {
$words .= " and " . self::numberToWords($pesewas) . " Pesewas";
}
return $words;
}
private static function numberToWords($num) {
// 标准数字转英文单词逻辑
// 可参考现有库如 NumberToWords
return (new NumberToWords\NumberToWords())->transform($num, 'en');
}
}
完整使用示例
// 用户输入 $userInput = "eGH¢ 250.75"; // 规范化处理 $ghsAmount = CurrencyDetector::normalizeToGHS($userInput, 1.02); echo "实际GHS金额: " . GhanaCediFormatter::formatGHS($ghsAmount); // 输出: GH¢ 245.83 // 显示原始eGHS $detected = CurrencyDetector::detectAndConvert($userInput); echo "原始eGHS: " . GhanaCediFormatter::formatEGHS($detected['amount']); // 输出: eGH¢ 250.75
关键注意事项
- 汇率波动:eGHS与GHS通常1:1,但实际项目中需定期从加纳银行获取实时汇率
- 最小单位:加纳货币最小单位为1 pesewa = 0.01 GHS
- 显示格式:使用
GH¢前缀(Cedi符号),eGHS建议用eGH¢区分 - 精度控制:所有计算使用
DECIMAL或float,显示时保留2位小数
此方案完整支持加纳数字货币与传统货币的混合处理,可根据实际业务需求调整汇率逻辑。