@@ -26,23 +26,28 @@ public static function format($value, string $format): string
2626 $ decimalLength = strlen ($ decimalPart );
2727 $ decimalDivisor = 10 ** $ decimalLength ;
2828
29- /** @var float */
30- $ GCD = MathTrig \Gcd::evaluate ($ decimalPart , $ decimalDivisor );
31- /** @var float */
32- $ decimalPartx = $ decimalPart ;
29+ preg_match ('/(#?.*\?)\/(\?+|\d+)/ ' , $ format , $ matches );
30+ $ formatIntegerPart = $ matches [1 ];
3331
34- $ adjustedDecimalPart = $ decimalPartx / $ GCD ;
35- $ adjustedDecimalDivisor = $ decimalDivisor / $ GCD ;
32+ if (is_numeric ($ matches [2 ])) {
33+ $ fractionDivisor = 100 / (int ) $ matches [2 ];
34+ } else {
35+ /** @var float */
36+ $ fractionDivisor = MathTrig \Gcd::evaluate ((int ) $ decimalPart , $ decimalDivisor );
37+ }
38+
39+ $ adjustedDecimalPart = (int ) round ((int ) $ decimalPart / $ fractionDivisor , 0 );
40+ $ adjustedDecimalDivisor = $ decimalDivisor / $ fractionDivisor ;
3641
37- if ((strpos ($ format , '0 ' ) !== false )) {
42+ if ((strpos ($ formatIntegerPart , '0 ' ) !== false )) {
3843 return "{$ sign }{$ integerPart } {$ adjustedDecimalPart }/ {$ adjustedDecimalDivisor }" ;
39- } elseif ((strpos ($ format , '# ' ) !== false )) {
44+ } elseif ((strpos ($ formatIntegerPart , '# ' ) !== false )) {
4045 if ($ integerPart == 0 ) {
4146 return "{$ sign }{$ adjustedDecimalPart }/ {$ adjustedDecimalDivisor }" ;
4247 }
4348
4449 return "{$ sign }{$ integerPart } {$ adjustedDecimalPart }/ {$ adjustedDecimalDivisor }" ;
45- } elseif ((substr ($ format , 0 , 3 ) == '? ? ' )) {
50+ } elseif ((substr ($ formatIntegerPart , 0 , 3 ) == '? ? ' )) {
4651 if ($ integerPart == 0 ) {
4752 $ integerPart = '' ;
4853 }
0 commit comments