Files
lotteryLaravel/app/Support/CurrencyFormatter.php
kang 8ccf39dff5 refactor: 迁移彩票设置至 LotterySettings 服务
- 更新多个控制器和服务,使用 LotterySettings 服务获取彩票相关配置,如默认币种、开奖间隔、下注窗口等,提升代码一致性与可维护性。
- 移除 .env.example 中不再使用的配置项,建议通过后台管理进行设置。
2026-05-28 14:50:25 +08:00

48 lines
1.4 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
namespace App\Support;
use App\Services\LotterySettings;
/**
* 将「最小货币单位」整数格式化为展示用字符串(不改变业务数字,仅格式化)。
*
* 拆分位数由 {@see config('lottery.ui.format.currency.decimals')} 决定,默认 2即 ÷100
*/
final class CurrencyFormatter
{
public static function fromMinor(int|string|null $minor): string
{
if ($minor === null || $minor === '') {
return self::formatMinorInt(0);
}
return self::formatMinorInt((int) $minor);
}
private static function formatMinorInt(int $minorUnits): string
{
$decimals = LotterySettings::currencyDisplayDecimals();
$decSep = LotterySettings::currencyDecimalSeparator();
$thousandsSep = LotterySettings::currencyThousandsSeparator();
$divisor = (int) max(1, 10 ** $decimals);
$negative = $minorUnits < 0;
$abs = abs($minorUnits);
$integerPart = intdiv($abs, $divisor);
$fractionRaw = $abs % $divisor;
$fractionPadded = str_pad((string) $fractionRaw, $decimals, '0', STR_PAD_LEFT);
$integerPartFormatted = number_format((float) $integerPart, 0, $decSep, $thousandsSep);
if ($decimals === 0) {
return ($negative ? '-' : '').$integerPartFormatted;
}
return ($negative ? '-' : '').$integerPartFormatted.$decSep.$fractionPadded;
}
}