Skip to content

Commit

Permalink
Use sprintf in Non-Locale-Aware Manner
Browse files Browse the repository at this point in the history
  • Loading branch information
oleibman committed Jul 30, 2024
1 parent 6de86f5 commit e39dfe3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/PhpSpreadsheet/Calculation/MathTrig/Round.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ public static function up($number, $digits): array|string|float
if ($digitsPlus1 < 0) {
return round($number - 0.5 * 0.1 ** $digits, $digits, PHP_ROUND_HALF_DOWN);
}
$result = sprintf("%.{$digitsPlus1}f", $number - 0.5 * 0.1 ** $digits);
$result = sprintf("%.{$digitsPlus1}F", $number - 0.5 * 0.1 ** $digits);

return round((float) $result, $digits, PHP_ROUND_HALF_DOWN);
}

if ($digitsPlus1 < 0) {
return round($number + 0.5 * 0.1 ** $digits, $digits, PHP_ROUND_HALF_DOWN);
}
$result = sprintf("%.{$digitsPlus1}f", $number + 0.5 * 0.1 ** $digits);
$result = sprintf("%.{$digitsPlus1}F", $number + 0.5 * 0.1 ** $digits);

return round((float) $result, $digits, PHP_ROUND_HALF_DOWN);
}
Expand Down Expand Up @@ -119,7 +119,7 @@ public static function down($number, $digits): array|string|float
if ($digitsPlus1 < 0) {
return round($number + 0.5 * 0.1 ** $digits, $digits, PHP_ROUND_HALF_UP);
}
$result = sprintf("%.{$digitsPlus1}f", $number + 0.5 * 0.1 ** $digits);
$result = sprintf("%.{$digitsPlus1}F", $number + 0.5 * 0.1 ** $digits);

return round((float) $result, $digits, PHP_ROUND_HALF_UP);
}
Expand All @@ -128,7 +128,7 @@ public static function down($number, $digits): array|string|float
return round($number - 0.5 * 0.1 ** $digits, $digits, PHP_ROUND_HALF_UP);
}

$result = sprintf("%.{$digitsPlus1}f", $number - 0.5 * 0.1 ** $digits);
$result = sprintf("%.{$digitsPlus1}F", $number - 0.5 * 0.1 ** $digits);

return round((float) $result, $digits, PHP_ROUND_HALF_UP);
}
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Calculation/MathTrig/Trunc.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static function evaluate(array|float|string|null $value = 0, array|int|st
return ($minusSign === '') ? $result : -$result;
}
$decimals = PHP_FLOAT_DIG - strlen((string) (int) $value);
$resultString = sprintf('%.' . $decimals . 'f', $value);
$resultString = sprintf('%.' . $decimals . 'F', $value);
$regExp = '/([.]\\d{' . $digits . '})\\d+$/';
$result = $minusSign . (preg_replace($regExp, '$1', $resultString) ?? $resultString);

Expand Down

0 comments on commit e39dfe3

Please sign in to comment.