diff options
author | Marek Kotewicz <marek.kotewicz@gmail.com> | 2015-01-16 23:26:58 +0800 |
---|---|---|
committer | Marek Kotewicz <marek.kotewicz@gmail.com> | 2015-01-16 23:26:58 +0800 |
commit | 774e9d24a14fd258bd832b10dab445ebaa792d29 (patch) | |
tree | 1100408cc73caa702b9a52999044ef0c99ee6ec1 /lib | |
parent | 9a264a4284ee622b3bf6a2640339afb810611766 (diff) | |
download | dexon-774e9d24a14fd258bd832b10dab445ebaa792d29.tar dexon-774e9d24a14fd258bd832b10dab445ebaa792d29.tar.gz dexon-774e9d24a14fd258bd832b10dab445ebaa792d29.tar.bz2 dexon-774e9d24a14fd258bd832b10dab445ebaa792d29.tar.lz dexon-774e9d24a14fd258bd832b10dab445ebaa792d29.tar.xz dexon-774e9d24a14fd258bd832b10dab445ebaa792d29.tar.zst dexon-774e9d24a14fd258bd832b10dab445ebaa792d29.zip |
abi.js rounds down floating point input
Diffstat (limited to 'lib')
-rw-r--r-- | lib/abi.js | 22 |
1 files changed, 11 insertions, 11 deletions
diff --git a/lib/abi.js b/lib/abi.js index fbd6effb6..11141e28b 100644 --- a/lib/abi.js +++ b/lib/abi.js @@ -28,6 +28,8 @@ if (process.env.NODE_ENV !== 'build') { var web3 = require('./web3'); // jshint ignore:line +BigNumber.config({ ROUNDING_MODE: BigNumber.ROUND_DOWN }); + // TODO: make these be actually accurate instead of falling back onto JS's doubles. var hexToDec = function (hex) { return parseInt(hex, 16).toString(); @@ -87,25 +89,23 @@ var setupInputTypes = function () { /// Formats input value to byte representation of int /// If value is negative, return it's two's complement + /// If the value is floating point, it rounds it down /// @returns right-aligned byte representation of int var formatInt = function (value) { var padding = 32 * 2; - if (value instanceof BigNumber) { + if (value instanceof BigNumber || typeof value === 'number') { + if (typeof value === 'number') + value = new BigNumber(value); + value = value.round(); + if (value.lessThan(0)) - value = new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(value).plus(1).toString(16); - else - value = value.toString(16); - } - else if (typeof value === 'number') { - if (value < 0) - value = new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(value).plus(1).toString(16); - else - value = new BigNumber(value).toString(16); + value = new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(value).plus(1); + value = value.toString(16); } else if (value.indexOf('0x') === 0) value = value.substr(2); else if (typeof value === 'string') - value = new BigNumber(value).toString(16); + value = formatInt(new BigNumber(value)); else value = (+value).toString(16); return padLeft(value, padding); |