bitsharesjs
bitsharesjs copied to clipboard
Function `to_long()` returns unexpected value when input is float with decimals
As mentioned in https://github.com/bitshares/bitshares-ui/issues/3545#issuecomment-1236207285, the function to_long() in SerializerValidation.js may return unexpected result if the input is a float with decimals (E.G. 12.999999999999998). Code: https://github.com/bitshares/bitsharesjs/blob/c962b80f625d550573e847e6f1466524109fd3ea/lib/serializer/src/SerializerValidation.js#L103-L116
> parseFloat("0.00013")*100000
12.999999999999998
> Long.fromString(""+parseFloat("0.00013")*100000)
Long { low: 1150981118, high: 30, unsigned: false }
> Long.fromString(""+parseFloat("0.00013")*100000).toString()
129999999998
To fix this, IMHO, as a library, we shouldn't change user input, but we should require the input to be valid.
For a little extra context, it looks like Long (from the bytebuffer library), will correctly handle the first five digits after the decimal point, whereafter it seems to just append digits.
> var Long = require("bytebuffer").Long;
undefined
> Long.fromString("12.12345")
Long { low: 12, high: 0, unsigned: false }
> Long.fromString("12.123456")
Long { low: 126, high: 0, unsigned: false }
> Long.fromString("12.1234567")
Long { low: 1267, high: 0, unsigned: false }
>