See Release Notes
Long Term Support Release
Differences Between: [Versions 400 and 401] [Versions 401 and 402] [Versions 401 and 403]
1 <?php 2 3 namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat; 4 5 use PhpOffice\PhpSpreadsheet\Style\NumberFormat; 6 7 class PercentageFormatter extends BaseFormatter 8 { 9 public static function format($value, string $format): string 10 { 11 if ($format === NumberFormat::FORMAT_PERCENTAGE) { 12 return round((100 * $value), 0) . '%'; 13 } 14 15 $value *= 100; 16 $format = self::stripQuotes($format); 17 18 [, $vDecimals] = explode('.', ((string) $value) . '.'); 19 $vDecimalCount = strlen(rtrim($vDecimals, '0')); 20 21 $format = str_replace('%', '%%', $format); 22 $wholePartSize = strlen((string) floor($value)); 23 $decimalPartSize = 0; 24 $placeHolders = ''; 25 // Number of decimals 26 if (preg_match('/\.([?0]+)/u', $format, $matches)) { 27 $decimalPartSize = strlen($matches[1]); 28 $vMinDecimalCount = strlen(rtrim($matches[1], '?')); 29 $decimalPartSize = min(max($vMinDecimalCount, $vDecimalCount), $decimalPartSize); 30 $placeHolders = str_repeat(' ', strlen($matches[1]) - $decimalPartSize); 31 } 32 // Number of digits to display before the decimal 33 if (preg_match('/([#0,]+)\.?/u', $format, $matches)) { 34 $firstZero = preg_replace('/^[#,]*/', '', $matches[1]); 35 $wholePartSize = max($wholePartSize, strlen($firstZero)); 36 } 37 38 $wholePartSize += $decimalPartSize + (int) ($decimalPartSize > 0); 39 $replacement = "0{$wholePartSize}.{$decimalPartSize}"; 40 $mask = (string) preg_replace('/[#0,]+\.?[?#0,]*/ui', "%{$replacement}f{$placeHolders}", $format); 41 42 /** @var float */ 43 $valueFloat = $value; 44 45 return sprintf($mask, round($valueFloat, $decimalPartSize)); 46 } 47 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body