PHP项目数字马赛法郎

wen PHP项目 1

本文目录导读:

PHP项目数字马赛法郎

  1. 马赛法郎数字格式化函数
  2. 更灵活的货币类(推荐项目使用)
  3. 数据库存储与显示建议
  4. 完整示例:报价单显示
  5. 注意事项

针对“PHP项目数字马赛法郎”这一需求,我理解你可能需要在PHP项目中处理马赛法郎数字格式化(例如金额显示、计算处理等),马赛法郎(Franc CFA)有特定的货币符号(FCFA、CFA、XAF/XOF等)和数字格式(千分位分隔符、小数位数等),下面我会提供一个完整的PHP实现方案,涵盖格式化、转换和安全处理。

马赛法郎数字格式化函数

<?php
/**
 * 马赛法郎数字格式化
 * @param float $amount 金额
 * @param string $locale 地区(xof:西非法郎, xaf:中非法郎)
 * @param bool $showSymbol 是否显示货币符号
 * @return string 格式化后的金额字符串
 */
function formatFCFA($amount, $locale = 'xof', $showSymbol = true) {
    // 马赛法郎的符号和ISO代码
    $symbols = [
        'xof' => 'CFA',  // 西非法郎
        'xaf' => 'FCFA', // 中非法郎
    ];
    // 默认使用西非法郎符号
    $currencySymbol = $symbols[$locale] ?? 'FCFA';
    // 马赛法郎固定为0小数(理论上没有分币)
    $formatted = number_format(
        $amount, 
        0,           // 小数位数:0
        '.',         // 小数点
        ' '          // 千分位分隔符(空格,法语常用)
    );
    // 也可以改为逗号隔每三位
    // $formatted = number_format($amount, 0, '.', ','); 
    if ($showSymbol) {
        // 通常格式:金额 + 空格 + 符号
        return $formatted . ' ' . $currencySymbol;
    }
    return $formatted;
}
// 使用示例
echo formatFCFA(1234567.89);  // 输出:1 234 568 FCFA
echo formatFCFA(500, 'xof');  // 输出:500 CFA
echo formatFCFA(1234567.89, 'xaf', false); // 输出:1 234 568

更灵活的货币类(推荐项目使用)

<?php
class FCFAFormatter {
    // 货币配置
    const CURRENCIES = [
        'XOF' => ['symbol' => 'CFA', 'locale' => 'fr_FR', 'decimals' => 0],
        'XAF' => ['symbol' => 'FCFA', 'locale' => 'fr_FR', 'decimals' => 0],
    ];
    private $currency;
    private $symbol;
    private $decimals;
    private $locale;
    public function __construct($currency = 'XOF') {
        $this->currency = strtoupper($currency);
        $config = self::CURRENCIES[$this->currency] ?? self::CURRENCIES['XOF'];
        $this->symbol = $config['symbol'];
        $this->decimals = $config['decimals'];
        $this->locale = $config['locale'];
    }
    /**
     * 格式化金额
     * @param float $amount
     * @param bool $showSymbol
     * @return string
     */
    public function format($amount, $showSymbol = true) {
        // 使用intl扩展可以获得本地化格式(需要安装ext-intl)
        if (extension_loaded('intl')) {
            $formatter = new NumberFormatter($this->locale, NumberFormatter::CURRENCY);
            $formatted = $formatter->formatCurrency($amount, $this->currency);
            // 注意:intl的formatCurrency会带符号和ISO代码,需要定制
            // 这里我们手动处理
        }
        // 手动格式化
        $formatted = number_format(
            $amount,
            $this->decimals,
            ',',    // 法语小数点:逗号
            ' '     // 千分位:空格
        );
        if ($showSymbol) {
            return $formatted . ' ' . $this->symbol;
        }
        return $formatted;
    }
    /**
     * 从字符串解析金额(忽略符号)
     * @param string $string
     * @return float
     */
    public function parse($string) {
        // 移除符号、空格、FCFA、CFA等
        $cleaned = preg_replace('/[^0-9\.,]/', '', $string);
        $cleaned = str_replace(',', '.', $cleaned); // 法语逗号转小数点
        return (float) $cleaned;
    }
    /**
     * 计算增值税(含税/不含税转换)
     * @param float $amount
     * @param float $rate 税率,如0.18
     * @param bool $isInclusive 是否含税
     * @return array
     */
    public function calculateTax($amount, $rate = 0.18, $isInclusive = false) {
        if ($isInclusive) {
            $tax = ($amount * $rate) / (1 + $rate);
            $exclTax = $amount - $tax;
        } else {
            $tax = $amount * $rate;
            $exclTax = $amount;
        }
        return [
            'excl_tax' => round($exclTax, 0),
            'tax' => round($tax, 0),
            'incl_tax' => round($exclTax + $tax, 0),
            'rate' => $rate,
        ];
    }
}
// 使用示例
$franch = new FCFAFormatter('XAF');
echo $franch->format(1234567);  // 1 234 567 FCFA
$west = new FCFAFormatter('XOF');
echo $west->format(500);       // 500 CFA
// 解析
$amount = $west->parse("1 500 CFA"); // 返回 1500.0
// 增值税计算
$taxInfo = $west->calculateTax(10000, 0.18, false);
echo $taxInfo['incl_tax']; // 11800

数据库存储与显示建议

// 数据库存储:使用整数(分)或 decimal(15,0)
// 马赛法郎通常没有分币,可以直接存整数
// 显示时格式化
function displayFCFA($dbAmount) {
    // 假设数据库存的是整数 CFA(无小数)
    return number_format($dbAmount, 0, '.', ' ') . ' FCFA';
}
// 用户输入处理
function sanitizeFCFAInput($input) {
    // 允许纯数字、空格、逗号
    $cleaned = preg_replace('/[^0-9\s,]/', '', $input);
    $cleaned = str_replace(',', '', $cleaned); // 移除逗号
    $cleaned = str_replace(' ', '', $cleaned); // 移除空格
    return (int) $cleaned; // 转为整数存储
}

完整示例:报价单显示

<?php
require_once 'FCFAFormatter.php';
class Invoice {
    private $items = [];
    private $formatter;
    public function __construct() {
        $this->formatter = new FCFAFormatter('XAF');
    }
    public function addItem($name, $price, $qty = 1) {
        $this->items[] = [
            'name' => $name,
            'price' => $price,
            'qty' => $qty,
            'total' => $price * $qty
        ];
    }
    public function render() {
        echo "<table border='1'>";
        echo "<tr><th>Article</th><th>PU (FCFA)</th><th>Qté</th><th>Total (FCFA)</th></tr>";
        $grandTotal = 0;
        foreach ($this->items as $item) {
            $total = $item['total'];
            $grandTotal += $total;
            echo "<tr>";
            echo "<td>" . htmlspecialchars($item['name']) . "</td>";
            echo "<td>" . $this->formatter->format($item['price']) . "</td>";
            echo "<td>" . $item['qty'] . "</td>";
            echo "<td>" . $this->formatter->format($total) . "</td>";
            echo "</tr>";
        }
        echo "<tr><td colspan='3'><strong>TOTAL</strong></td>";
        echo "<td><strong>" . $this->formatter->format($grandTotal) . "</strong></td></tr>";
        echo "</table>";
    }
}
// 使用
$invoice = new Invoice();
$invoice->addItem('Riz 5kg', 3500, 2);
$invoice->addItem('Huile 1L', 2500, 1);
$invoice->render();

注意事项

  1. 小数处理:马赛法郎理论上没有分币(最小单位是1 CFA),但实际业务中可能有折扣、税率计算导致小数,建议:

    • 存储时使用 decimal(15,2) 保留2位小数
    • 显示时根据业务需求取舍(四舍五入或截断)
    • 也可以用 int 存储整数CFA(避免浮点误差)
  2. 本地化差异

    • 法语区:千分位用空格,小数用逗号(如 1 234,50
    • 英语区:千分位用逗号,小数用点(如 1,234.50
    • 马赛法郎通常显示为整数
  3. 符号位置

    • 常见写法:金额 + 空格 + 符号(如 5 000 FCFA
    • 也有写成:符号 + 金额(如 CFA 5 000)——较少见
  4. 安全编码

    • 所有用户输入的数字需要清理(strip_tags、正则过滤)
    • 数据库查询使用参数绑定防止SQL注入
    • 显示时对金额使用 htmlspecialchars() 防止XSS

如果需要更具体的功能(比如汇率换算、税务计算、大额数字中文大写等),请告诉我你的具体使用场景!

抱歉,评论功能暂时关闭!