From 4fd5d1ab670d1ac27950d056de9f6ebd67e3ebc3 Mon Sep 17 00:00:00 2001 From: sc0Vu Date: Sun, 27 May 2018 22:48:02 +0800 Subject: [PATCH] Fix #83 Return $fractionLength when number is with decimal point. --- src/Utils.php | 5 ++--- test/unit/UtilsTest.php | 40 +++++++++++++++++++++++++++++----------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/Utils.php b/src/Utils.php index fde4c502..fcb49640 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -284,9 +284,7 @@ public static function toWei($number, $unit) if (is_array($bn)) { // fraction number - list($whole, $fraction, $negative1) = $bn; - - $fractionLength = strlen($fraction->toString()); + list($whole, $fraction, $fractionLength, $negative1) = $bn; if ($fractionLength > strlen(self::UNITS[$unit])) { throw new InvalidArgumentException('toWei fraction part is out of limit.'); @@ -504,6 +502,7 @@ public static function toBn($number) return [ new BigNumber($whole), new BigNumber($fraction), + strlen($comps[1]), isset($negative1) ? $negative1 : false ]; } else { diff --git a/test/unit/UtilsTest.php b/test/unit/UtilsTest.php index 5856b227..e3e1e297 100644 --- a/test/unit/UtilsTest.php +++ b/test/unit/UtilsTest.php @@ -275,12 +275,24 @@ public function testToWei() $bn = Utils::toWei('1.69', 'ether'); $this->assertEquals($bn->toString(), '1690000000000000000'); + $bn = Utils::toWei('0.01', 'ether'); + $this->assertEquals($bn->toString(), '10000000000000000'); + + $bn = Utils::toWei('0.002', 'ether'); + $this->assertEquals($bn->toString(), '2000000000000000'); + $bn = Utils::toWei(0.1, 'ether'); $this->assertEquals($bn->toString(), '100000000000000000'); $bn = Utils::toWei(1.69, 'ether'); $this->assertEquals($bn->toString(), '1690000000000000000'); + $bn = Utils::toWei(0.01, 'ether'); + $this->assertEquals($bn->toString(), '10000000000000000'); + + $bn = Utils::toWei(0.002, 'ether'); + $this->assertEquals($bn->toString(), '2000000000000000'); + $bn = Utils::toWei('-0.1', 'ether'); $this->assertEquals($bn->toString(), '-100000000000000000'); @@ -541,39 +553,45 @@ public function testToBn() $this->assertEquals($bn->toString(), '-1'); $bn = Utils::toBn('-0.1'); - $this->assertEquals(count($bn), 3); + $this->assertEquals(count($bn), 4); $this->assertEquals($bn[0]->toString(), '0'); $this->assertEquals($bn[1]->toString(), '1'); - $this->assertEquals($bn[2]->toString(), '-1'); + $this->assertEquals($bn[2], 1); + $this->assertEquals($bn[3]->toString(), '-1'); $bn = Utils::toBn(-0.1); - $this->assertEquals(count($bn), 3); + $this->assertEquals(count($bn), 4); $this->assertEquals($bn[0]->toString(), '0'); $this->assertEquals($bn[1]->toString(), '1'); - $this->assertEquals($bn[2]->toString(), '-1'); + $this->assertEquals($bn[2], 1); + $this->assertEquals($bn[3]->toString(), '-1'); $bn = Utils::toBn('0.1'); - $this->assertEquals(count($bn), 3); + $this->assertEquals(count($bn), 4); $this->assertEquals($bn[0]->toString(), '0'); $this->assertEquals($bn[1]->toString(), '1'); - $this->assertEquals($bn[2], false); + $this->assertEquals($bn[2], 1); + $this->assertEquals($bn[3], false); $bn = Utils::toBn('-1.69'); - $this->assertEquals(count($bn), 3); + $this->assertEquals(count($bn), 4); $this->assertEquals($bn[0]->toString(), '1'); $this->assertEquals($bn[1]->toString(), '69'); - $this->assertEquals($bn[2]->toString(), '-1'); + $this->assertEquals($bn[2], 2); + $this->assertEquals($bn[3]->toString(), '-1'); $bn = Utils::toBn(-1.69); $this->assertEquals($bn[0]->toString(), '1'); $this->assertEquals($bn[1]->toString(), '69'); - $this->assertEquals($bn[2]->toString(), '-1'); + $this->assertEquals($bn[2], 2); + $this->assertEquals($bn[3]->toString(), '-1'); $bn = Utils::toBn('1.69'); - $this->assertEquals(count($bn), 3); + $this->assertEquals(count($bn), 4); $this->assertEquals($bn[0]->toString(), '1'); $this->assertEquals($bn[1]->toString(), '69'); - $this->assertEquals($bn[2], false); + $this->assertEquals($bn[2], 2); + $this->assertEquals($bn[3], false); $bn = Utils::toBn(new BigNumber(1)); $this->assertEquals($bn->toString(), '1');