From 9f6c0d8aa17bc0e197c03f22550dae596c24e19f Mon Sep 17 00:00:00 2001 From: justmd5 Date: Sat, 7 Sep 2024 22:44:43 +0800 Subject: [PATCH] =?UTF-8?q?fix=20#19=20Decimal=E7=9B=B8=E5=85=B3=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E5=81=B6=E5=8F=91=E4=B8=A2=E5=A4=B1=E7=B2=BE=E5=BA=A6?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 6 ++---- src/Types.php | 29 ++++++++--------------------- 2 files changed, 10 insertions(+), 25 deletions(-) diff --git a/composer.json b/composer.json index 3570a98..56c1019 100644 --- a/composer.json +++ b/composer.json @@ -17,14 +17,12 @@ ], "require": { "php": ">=7.2", - "ext-json": "*" + "ext-json": "*", + "ext-bcmath":"*" }, "autoload": { "psr-4": { "OneCk\\": "src/" } - }, - "suggest": { - "ext-bcmath": "Supported Decimal128" } } diff --git a/src/Types.php b/src/Types.php index 353f47f..e882c1b 100644 --- a/src/Types.php +++ b/src/Types.php @@ -459,17 +459,10 @@ protected function format(&$data, $type) $fn = $call[$type]; } else if (self::isDecimal($type)) { $arr = explode(',', substr($type, 8, -1)); - if ($arr[0] >= 19) { - $fn = function ($v) use ($arr) { - $tr = explode('.', "{$v}"); - isset($tr[1]) || $tr[1] = ''; - return $tr[0] . $tr[1] . str_repeat('0', max(0, $arr[1] - strlen($tr[1]))); - }; - } else { - $fn = function ($v) use ($arr) { - return $v * pow(10, $arr[1]); - }; - } + $scale = intval($arr[1]); + $fn = function ($v) use ($scale) { + return bcmul($v, bcpow('10', $scale, 0), 0); + }; } else if (self::isDatetime64($type)) { $n = substr($type, 11, -1); $fn = function ($v) use ($n) { @@ -513,16 +506,10 @@ protected function unFormat($type) $fn = $call[$type]; } else if (self::isDecimal($type)) { $arr = explode(',', substr($type, 8, -1)); - if ($arr[0] >= 19) { - $fn = function ($v) use ($arr) { - $v = "{$v}"; - return substr($v, 0, -$arr[1]) . '.' . substr($v, -$arr[1]); - }; - } else { - $fn = function ($v) use ($arr) { - return $v / pow(10, $arr[1]); - }; - } + $scale = intval($arr[1]); + $fn = function ($v) use ($scale) { + return bcdiv($v, bcpow('10', $scale, 0), $scale); + }; } else if (self::isDatetime64($type)) { $fn = function ($v) { return date('Y-m-d H:i:s', substr($v, 0, 10)) . '.' . substr($v, 10);