|
1 | 1 | // Package decimal with support of Tarantool's decimal data type.
|
2 | 2 | //
|
| 3 | +// The package implements methods to encode and decode BCD. |
| 4 | +// |
| 5 | +// BCD (Binary-Coded Decimal) is a sequence of bytes representing decimal |
| 6 | +// digits of the encoded number (each byte has two decimal digits each encoded |
| 7 | +// using 4-bit nibbles), so byte >> 4 is the first digit and byte & 0x0f is the |
| 8 | +// second digit. The leftmost digit in the array is the most significant. The |
| 9 | +// rightmost digit in the array is the least significant. |
| 10 | +// |
| 11 | +// The first byte of the BCD array contains the first digit of the number, |
| 12 | +// represented as follows: |
| 13 | +// |
| 14 | +// | 4 bits | 4 bits | |
| 15 | +// = 0x = the 1st digit |
| 16 | +// |
| 17 | +// (The first nibble contains 0 if the decimal number has an even number of |
| 18 | +// digits). The last byte of the BCD array contains the last digit of the |
| 19 | +// number and the final nibble, represented as follows: |
| 20 | +// |
| 21 | +// | 4 bits | 4 bits | |
| 22 | +// = the last digit = nibble |
| 23 | +// |
| 24 | +// The final nibble represents the number's sign: 0x0a, 0x0c, 0x0e, 0x0f stand |
| 25 | +// for plus, 0x0b and 0x0d stand for minus. |
| 26 | +// |
| 27 | +// Examples: |
| 28 | +// |
| 29 | +// The decimal -12.34 will be encoded as 0xd6, 0x01, 0x02, 0x01, 0x23, 0x4d: |
| 30 | +// |
| 31 | +// | MP_EXT (fixext 4) | MP_DECIMAL | scale | 1 | 2,3 | 4 (minus) | |
| 32 | +// | 0xd6 | 0x01 | 0x02 | 0x01 | 0x23 | 0x4d | |
| 33 | +// |
| 34 | +// The decimal 0.000000000000000000000000000000000010 will be encoded as |
| 35 | +// 0xc7, 0x03, 0x01, 0x24, 0x01, 0x0c: |
| 36 | +// |
| 37 | +// | MP_EXT (ext 8) | length | MP_DECIMAL | scale | 1 | 0 (plus) | |
| 38 | +// | 0xc7 | 0x03 | 0x01 | 0x24 | 0x01 | 0x0c | |
| 39 | +// |
3 | 40 | // Decimal data type supported in Tarantool since 2.2.
|
4 | 41 | //
|
5 | 42 | // Since: 1.7.0
|
|
13 | 50 | // * Tarantool issue for support decimal type https://github.com/tarantool/tarantool/issues/692
|
14 | 51 | //
|
15 | 52 | // * Tarantool module decimal https://www.tarantool.io/en/doc/latest/reference/reference_lua/decimal/
|
| 53 | +// |
| 54 | +// * An implementation in C language https://github.com/tarantool/decNumber/blob/master/decPacked.c |
16 | 55 | package decimal
|
17 | 56 |
|
18 | 57 | import (
|
@@ -79,9 +118,9 @@ func (decNum *Decimal) MarshalMsgpack() ([]byte, error) {
|
79 | 118 | // buffer length is not fixed and encoded by a number in a separate
|
80 | 119 | // field:
|
81 | 120 | //
|
82 |
| -// +--------+-------------------+------------+===============+ |
83 |
| -// | MP_EXT | length (optional) | MP_DECIMAL | PackedDecimal | |
84 |
| -// +--------+-------------------+------------+===============+ |
| 121 | +// +--------+-------------------+------------+===============+ |
| 122 | +// | MP_EXT | length (optional) | MP_DECIMAL | PackedDecimal | |
| 123 | +// +--------+-------------------+------------+===============+ |
85 | 124 | func (decNum *Decimal) UnmarshalMsgpack(b []byte) error {
|
86 | 125 | digits, err := decodeStringFromBCD(b)
|
87 | 126 | if err != nil {
|
|
0 commit comments