Skip to content

Commit

Permalink
Merge pull request #20 from justmd5/master
Browse files Browse the repository at this point in the history
fix #19 Decimal相关类型偶发丢失精度问题修复
  • Loading branch information
lizhichao authored Sep 9, 2024
2 parents aa02927 + 9f6c0d8 commit c7d9dc2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 25 deletions.
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@
],
"require": {
"php": ">=7.2",
"ext-json": "*"
"ext-json": "*",
"ext-bcmath":"*"
},
"autoload": {
"psr-4": {
"OneCk\\": "src/"
}
},
"suggest": {
"ext-bcmath": "Supported Decimal128"
}
}
29 changes: 8 additions & 21 deletions src/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit c7d9dc2

Please sign in to comment.