You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
795 B
PHP
25 lines
795 B
PHP
<?php
|
|
namespace App\Services\Cloud;
|
|
|
|
final class CloudMoney
|
|
{
|
|
public static function cents($value): int
|
|
{
|
|
if (!is_string($value) && !is_int($value)) {
|
|
throw new CloudError('金额必须使用十进制字符串');
|
|
}
|
|
if (!preg_match('/^(0|[1-9][0-9]{0,9})(?:\.([0-9]{1,2}))?$/D', (string) $value, $match)) {
|
|
throw new CloudError('金额格式无效');
|
|
}
|
|
return ((int) $match[1]) * 100 + (int) str_pad($match[2] ?? '', 2, '0');
|
|
}
|
|
|
|
public static function decimal(int $cents): string
|
|
{
|
|
if ($cents < 0 || $cents > 999999999999) {
|
|
throw new CloudError('金额超出支持范围');
|
|
}
|
|
return intdiv($cents, 100) . '.' . str_pad((string) ($cents % 100), 2, '0', STR_PAD_LEFT);
|
|
}
|
|
}
|