Proje dosyaları eklendi
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Serp\Operations;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
final class Money
|
||||
{
|
||||
public static function micros(string|int|float|null $value): int
|
||||
{
|
||||
if ($value === null) {
|
||||
return 0;
|
||||
}$s = is_float($value) ? number_format($value, 6, '.', '') : (string) $value;
|
||||
if (! preg_match('/^-?\d+(?:\.\d{1,6})?$/', $s)) {
|
||||
throw new InvalidArgumentException('Invalid money value.');
|
||||
}$negative = str_starts_with($s, '-');
|
||||
$s = ltrim($s, '-');
|
||||
[$whole,$fraction] = array_pad(explode('.', $s, 2), 2, '');
|
||||
$result = ((int) $whole * 1000000) + (int) str_pad($fraction, 6, '0');
|
||||
|
||||
return $negative ? -$result : $result;
|
||||
}
|
||||
|
||||
public static function decimal(int $micros): string
|
||||
{
|
||||
$negative = $micros < 0;
|
||||
$micros = abs($micros);
|
||||
|
||||
return ($negative ? '-' : '').intdiv($micros, 1000000).'.'.str_pad((string) ($micros % 1000000), 6, '0', STR_PAD_LEFT);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user