DaitoNumber.php
5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
namespace Daito\Lib;
use Brick\Math\BigDecimal;
use Brick\Math\RoundingMode;
use RuntimeException;
class DaitoNumber
{
const DEFAULT_MAX_DECIMALS = 12;
/**
* Format number with configurable separators, prefix/suffix, and decimal behavior.
*
* Example:
* DaitoNumber::format('1234567.00') => '1,234,567'
* DaitoNumber::format('1234567.5', array('decimals' => 2)) => '1,234,567.50'
*/
public static function format($number, array $arrOptions = array())
{
$arrConfig = array_merge(
array(
'thousands_separator' => ',',
'decimal_separator' => '.',
'prefix' => '',
'suffix' => '',
'decimals' => null,
'trim_trailing_zeros' => true,
'max_decimals' => self::DEFAULT_MAX_DECIMALS,
),
$arrOptions
);
$numberString = self::toCanonicalString($number);
$isNegative = strpos($numberString, '-') === 0;
$unsigned = $isNegative ? substr($numberString, 1) : $numberString;
if ($arrConfig['decimals'] !== null) {
$decimals = (int) $arrConfig['decimals'];
if ($decimals < 0) {
throw new RuntimeException('decimals must be greater than or equal to 0.');
}
$unsigned = (string) BigDecimal::of($unsigned)->toScale($decimals, RoundingMode::HALF_UP);
} else {
$maxDecimals = (int) $arrConfig['max_decimals'];
if ($maxDecimals < 0) {
throw new RuntimeException('max_decimals must be greater than or equal to 0.');
}
$unsigned = (string) BigDecimal::of($unsigned)->toScale($maxDecimals, RoundingMode::HALF_UP);
if ($arrConfig['trim_trailing_zeros']) {
$unsigned = self::trimTrailingZeros($unsigned);
}
}
$arrParts = explode('.', $unsigned, 2);
$integerPart = self::addThousandsSeparator($arrParts[0], (string) $arrConfig['thousands_separator']);
$decimalPart = isset($arrParts[1]) ? $arrParts[1] : '';
$formattedNumber = $integerPart;
if ($decimalPart !== '') {
$formattedNumber .= (string) $arrConfig['decimal_separator'] . $decimalPart;
}
$formatted = (string) $arrConfig['prefix'] . $formattedNumber . (string) $arrConfig['suffix'];
return $isNegative ? '-' . $formatted : $formatted;
}
/**
* Format currency-friendly output (default 2 decimals, keep trailing zeros).
*
* Example:
* DaitoNumber::formatCurrency('1234.5') => '1,234.50'
* DaitoNumber::formatCurrency('1234.5', array('prefix' => '$')) => '$1,234.50'
*/
public static function formatCurrency($number, array $arrOptions = array())
{
$arrCurrencyOptions = array_merge(
array(
'decimals' => 2,
'trim_trailing_zeros' => false,
'prefix' => '',
'suffix' => '',
),
$arrOptions
);
return self::format($number, $arrCurrencyOptions);
}
/**
* Format percentage value.
*
* Example:
* DaitoNumber::formatPercent('12.3456') => '12.35%'
* DaitoNumber::formatPercent('0.1234', array('input_ratio' => true)) => '12.34%'
*/
public static function formatPercent($number, array $arrOptions = array())
{
$arrPercentOptions = array_merge(
array(
'decimals' => 2,
'trim_trailing_zeros' => true,
'suffix' => '%',
'input_ratio' => false,
),
$arrOptions
);
$numberValue = $number;
if ($arrPercentOptions['input_ratio']) {
$numberValue = (string) BigDecimal::of(self::toCanonicalString($number))->multipliedBy('100');
}
unset($arrPercentOptions['input_ratio']);
return self::format($numberValue, $arrPercentOptions);
}
private static function toCanonicalString($number)
{
if (is_int($number) || is_string($number)) {
$numberString = trim((string) $number);
} elseif (is_float($number)) {
$numberString = self::trimTrailingZeros(sprintf('%.14F', $number));
} else {
throw new RuntimeException('DaitoNumber only supports int, float, or numeric string.');
}
if ($numberString === '' || !preg_match('/^[+-]?\d+(\.\d+)?$/', $numberString)) {
throw new RuntimeException('Invalid number format.');
}
return (string) BigDecimal::of($numberString);
}
private static function addThousandsSeparator($integerPart, $thousandsSeparator)
{
return preg_replace('/\B(?=(\d{3})+(?!\d))/', $thousandsSeparator, $integerPart);
}
private static function trimTrailingZeros($numberString)
{
if (strpos($numberString, '.') === false) {
return $numberString;
}
$numberString = rtrim($numberString, '0');
$numberString = rtrim($numberString, '.');
return $numberString === '' ? '0' : $numberString;
}
}