Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #19 Decimal相关类型偶发丢失精度问题修复 #20

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading