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

Consider adding support for packed BCD #26

Open
rybakit opened this issue Sep 30, 2019 · 2 comments
Open

Consider adding support for packed BCD #26

rybakit opened this issue Sep 30, 2019 · 2 comments

Comments

@rybakit
Copy link

rybakit commented Sep 30, 2019

Hi!
I work on a msgpack extension that can pack/unpack fixed-point decimals that are stored in the following format:

+-------+--------------------------------+
| scale | packed BCD (2 digits per byte) |
+-------+--------------------------------+

I implemented packing/unpacking logic in pure PHP, but wonder if it would make sense to add support of packed decimals to this extension. Wdyt?

For example, here is how I pack them now:

$data = $value->toFixed(self::PRECISION);

if ('-' === $data[0]) {
    $nibble = 'd';
    $data = \substr($data, 1);
} else {
    $nibble = 'c';
}

$pieces = \explode('.', $data, 2);
$pieces[1] = \rtrim($pieces[1], '0');

$data = "{$pieces[0]}{$pieces[1]}{$nibble}";
if (0 !== \strlen($data) % 2) {
    $data = '0'.$data;
}

$scale = empty($pieces[1]) ? 0 : \strlen($pieces[1]);

return [$scale, \hex2bin($data)];
@rtheunissen
Copy link
Contributor

How does this compare or relate to how mpdecimal stores decimal values?

@rybakit
Copy link
Author

rybakit commented Dec 2, 2020

I'm not familiar with mpdecimal and don't know how it stores decimals, but if I'm not mistaken, this is implemented in the decNumber library. Maybe this link will explain better what I need:

And some more links to the implementation:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants