Closed
Description
Bug Report
Versions
- Driver: master
- Database:
- Java: 11
- OS: Linux
Current Behavior
The BigDecimal 0.99 is decoded as 9900.00. The problem occurs for each big decimal between -1 and 1.
In the other cases works correctly.
Steps to reproduce
To reproduce, please run
short[] array = new short[]{1, -1, 0, 2, 9900}
ByteBuf byteBuf = TestByteBufAllocator.TEST.buffer();
//BigDecimal 0.99
short[] array = new short[]{1, -1, 0, 2, 9900};
for (int i = 0; i < array.length; i++) {
byteBuf.writeShort(array[i]);
}
BigDecimal bigDecimal = NumericDecodeUtils.decodeBinary(byteBuf);
System.out.println(bigDecimal);
Expected behavior/code
0.99 istead of 9900.00
Possible Solution
The problem is that a negative weight is not handled.
As stated in https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/numeric.c#L263
Note: the first digit of a NumericVar's value is assumed to be multiplied
by NBASE ** weight. Another way to say it is that there are weight+1
digits before the decimal point. It is possible to have weight < 0.