Skip to content

Commit

Permalink
[9.x] Fix decimal cast, again (#45602)
Browse files Browse the repository at this point in the history
* Fix decimal cast, again

* code style

* Wrap exception in first party exception
  • Loading branch information
timacdonald authored Jan 12, 2023
1 parent 00d2058 commit 60c177b
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Illuminate\Database\Eloquent\Concerns;

use BackedEnum;
use Brick\Math\BigDecimal;
use Brick\Math\Exception\MathException as BrickMathException;
use Brick\Math\RoundingMode;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use DateTimeImmutable;
Expand All @@ -23,6 +26,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection as BaseCollection;
use Illuminate\Support\Exceptions\MathException;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Str;
Expand All @@ -31,8 +35,6 @@
use ReflectionClass;
use ReflectionMethod;
use ReflectionNamedType;
use RuntimeException;
use TypeError;

trait HasAttributes
{
Expand Down Expand Up @@ -1322,21 +1324,11 @@ public function fromFloat($value)
*/
protected function asDecimal($value, $decimals)
{
if (extension_loaded('bcmath')) {
return bcadd($value, 0, $decimals);
}

if (! is_numeric($value)) {
throw new TypeError('$value must be numeric.');
}

if (is_string($value) && Str::contains($value, 'e', true)) {
throw new RuntimeException('The "decimal" model cast is unable to handle string based floats with exponents.');
try {
return (string) BigDecimal::of($value)->toScale($decimals, RoundingMode::HALF_UP);
} catch (BrickMathException $e) {
throw new MathException('Unable to cast value to a decimal.', previous: $e);
}

[$int, $fraction] = explode('.', $value) + [1 => ''];

return Str::of($int)->padLeft('1', '0').'.'.Str::of($fraction)->limit($decimals, '')->padRight($decimals, '0');
}

/**
Expand Down

0 comments on commit 60c177b

Please sign in to comment.