Coder-Spirit / php-bignumbers

A robust library to handle immutable big numbers inside PHP applications
MIT License
131 stars 29 forks source link

fix Decimal::fromFloat() with specified lower scale #67

Open PetrHeinz opened 5 years ago

PetrHeinz commented 5 years ago

Hello, I've experienced some strange behavior of Decimal::fromFloat() when lowering the scale using the second parameter. I've provided additional tests and fixed the behavior using innerRound so it's consistent with Decimal::fromString().

Even though the last commit in this repo was created quite a while ago, I hope we can get it merged and released. Let me know if there's anything I can do to ease the merge process 🙂

The console output of the added tests with the current behavior describing the issue:

1) DecimalFromFloatTest::testFromFloat with data set #6 (1.000001, '1.00', 2)
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'1.00'
+'1.000001'

2) DecimalFromFloatTest::testFromFloat with data set #7 (-1.000001, '-1.00', 2)
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'-1.00'
+'-1.000001'

3) DecimalFromFloatTest::testFromFloat with data set #8 (0, '0', 0)
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'0'
+'0.'

4) DecimalFromFloatTest::testFromFloat with data set #11 (0.5, '1', 0)
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'1'
+'0.5'

5) DecimalFromFloatTest::testFromFloat with data set #12 (0.05, '0.1', 1)
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'0.1'
+'0.05'

6) DecimalFromFloatTest::testFromFloat with data set #13 (0.005, '0.01', 2)
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'0.01'
+'0.005'

7) DecimalFromFloatTest::testFromFloat with data set #14 (-0.5, '-1', 0)
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'-1'
+'-0.5'

8) DecimalFromFloatTest::testFromFloat with data set #15 (-0.05, '-0.1', 1)
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'-0.1'
+'-0.05'

9) DecimalFromFloatTest::testFromFloat with data set #16 (-0.005, '-0.01', 2)
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'-0.01'
+'-0.005'