Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

RateCalculator.php 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Utilities;
  3. use App\Enums\Setting\NumberFormat;
  4. use App\Models\Setting\Localization;
  5. class RateCalculator
  6. {
  7. public const PRECISION = 4;
  8. public const SCALING_FACTOR = 10 ** self::PRECISION;
  9. public const PERCENTAGE_SCALING_FACTOR = self::SCALING_FACTOR * 100;
  10. public static function calculatePercentage(int $value, int $scaledRate): int
  11. {
  12. return (int) round(($value * $scaledRate) / self::PERCENTAGE_SCALING_FACTOR);
  13. }
  14. public static function scaledRateToDecimal(int $scaledRate): float
  15. {
  16. return $scaledRate / self::PERCENTAGE_SCALING_FACTOR;
  17. }
  18. public static function decimalToScaledRate(float $decimalRate): int
  19. {
  20. return (int) round($decimalRate * self::PERCENTAGE_SCALING_FACTOR);
  21. }
  22. public static function parseLocalizedRate(string $value): int
  23. {
  24. $format = Localization::firstOrFail()->number_format->value;
  25. [$decimalMark, $thousandsSeparator] = NumberFormat::from($format)->getFormattingParameters();
  26. $floatValue = (float) str_replace([$thousandsSeparator, $decimalMark], ['', '.'], $value);
  27. return (int) round($floatValue * self::SCALING_FACTOR);
  28. }
  29. public static function formatScaledRate(int $scaledRate): string
  30. {
  31. $format = Localization::firstOrFail()->number_format->value;
  32. [$decimalMark, $thousandsSeparator] = NumberFormat::from($format)->getFormattingParameters();
  33. $percentageValue = $scaledRate / self::SCALING_FACTOR;
  34. $formatted = number_format($percentageValue, self::PRECISION, $decimalMark, $thousandsSeparator);
  35. $formatted = rtrim($formatted, '0');
  36. return rtrim($formatted, $decimalMark);
  37. }
  38. }