Number formatting further refactoring (#1082)

* Merge branch 'master' of C:\Projects\PHPOffice\PHPSpreadsheet\develop with conflicts.

* further refactoring of number formatting logic
This commit is contained in:
Mark Baker 2019-07-15 23:51:04 +02:00 committed by GitHub
parent 20f36ccd79
commit 987147e916
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -618,6 +618,38 @@ class NumberFormat extends Supervisor
return (($sign) ? '-' : '') . $result; return (($sign) ? '-' : '') . $result;
} }
private static function formatStraightNumericValue($value, $format, array $matches, $useThousands, $number_regex)
{
$left = $matches[1];
$dec = $matches[2];
$right = $matches[3];
// minimun width of formatted number (including dot)
$minWidth = strlen($left) + strlen($dec) + strlen($right);
if ($useThousands) {
$value = number_format(
$value,
strlen($right),
StringHelper::getDecimalSeparator(),
StringHelper::getThousandsSeparator()
);
$value = preg_replace($number_regex, $value, $format);
} else {
if (preg_match('/[0#]E[+-]0/i', $format)) {
// Scientific format
$value = sprintf('%5.2E', $value);
} elseif (preg_match('/0([^\d\.]+)0/', $format) || substr_count($format, '.') > 1) {
$value = self::complexNumberFormatMask($value, $format);
} else {
$sprintf_pattern = "%0$minWidth." . strlen($right) . 'f';
$value = sprintf($sprintf_pattern, $value);
$value = preg_replace($number_regex, $value, $format);
}
}
return $value;
}
private static function formatAsNumber($value, $format) private static function formatAsNumber($value, $format)
{ {
if ($format === self::FORMAT_CURRENCY_EUR_SIMPLE) { if ($format === self::FORMAT_CURRENCY_EUR_SIMPLE) {
@ -658,10 +690,8 @@ class NumberFormat extends Supervisor
// scale number // scale number
$value = $value / $scale; $value = $value / $scale;
// Strip # // Strip #
$format = preg_replace('/\\#/', '0', $format); $format = preg_replace('/\\#/', '0', $format);
// Remove locale code [$-###] // Remove locale code [$-###]
$format = preg_replace('/\[\$\-.*\]/', '', $format); $format = preg_replace('/\[\$\-.*\]/', '', $format);
@ -669,32 +699,7 @@ class NumberFormat extends Supervisor
$m = preg_replace($n, '', $format); $m = preg_replace($n, '', $format);
$number_regex = '/(0+)(\\.?)(0*)/'; $number_regex = '/(0+)(\\.?)(0*)/';
if (preg_match($number_regex, $m, $matches)) { if (preg_match($number_regex, $m, $matches)) {
$left = $matches[1]; $value = self::formatStraightNumericValue($value, $format, $matches, $useThousands, $number_regex);
$dec = $matches[2];
$right = $matches[3];
// minimun width of formatted number (including dot)
$minWidth = strlen($left) + strlen($dec) + strlen($right);
if ($useThousands) {
$value = number_format(
$value,
strlen($right),
StringHelper::getDecimalSeparator(),
StringHelper::getThousandsSeparator()
);
$value = preg_replace($number_regex, $value, $format);
} else {
if (preg_match('/[0#]E[+-]0/i', $format)) {
// Scientific format
$value = sprintf('%5.2E', $value);
} elseif (preg_match('/0([^\d\.]+)0/', $format) || substr_count($format, '.') > 1) {
$value = self::complexNumberFormatMask($value, $format);
} else {
$sprintf_pattern = "%0$minWidth." . strlen($right) . 'f';
$value = sprintf($sprintf_pattern, $value);
$value = preg_replace($number_regex, $value, $format);
}
}
} }
} }