2

I have an issue with laravel or php (don´t know yet), the issue is the next:

I have a field on my database called debe which value is 3.97 as you can see below. This field is a double(11,2).

sql table row

When I access to that field on my framework, for example using a dd() it returns the value correctly as you can see below.

dd result

The main problem occurs when i'm trying to print into the view, I receive the next:

value printed

I dont know why this is happening but right now the only solution that I've found is to round the value using PHP round() function.

Regards

Bagus Tesa
  • 1,317
  • 2
  • 19
  • 42
Parga
  • 23
  • 5
  • 3
    Could you post your code? And Your question is why the `dd` output is collection? – lighter Oct 18 '19 at 08:35
  • 1
    how about using `number_format` ? – Taha Paksu Oct 18 '19 at 08:36
  • 1
    @lighter My question is why php or laravel is formatting the field, the field in the database is 3.97 and the question is why I get 3.96999999998 and no 3.97 – Parga Oct 18 '19 at 08:41
  • 1
    The problem is just number's `Precision`. – lighter Oct 18 '19 at 08:43
  • @lighter and how do I fix it without any function that can modify its original value?? I have more fields with 3.98 and there is not any problem with them... – Parga Oct 18 '19 at 08:45
  • 2
    check https://stackoverflow.com/questions/6503994/problem-with-floating-values – Dilip Hirapara Oct 18 '19 at 08:48
  • I think you should use `round` to defined your precision is the best. Because you can't control this problem. – lighter Oct 18 '19 at 08:48
  • Maybe this isn't a perfect answer for your question, if your application is already in production state, but we had a similar problem in one of our projects too (crypto exchange). When using floats, numbers and values sometimes didn't match, since php does something weird with decimal points. Alternative was to store every number as integer/string, and we used BCMATH PHP library [link](https://www.php.net/manual/en/book.bc.php) to ensure the precision of our calculations. For now, everything seems pretty precise. So this might help you in future. – zlatan Oct 18 '19 at 09:36

1 Answers1

3

Check this article in the official documentation:

Floating point numbers have limited precision. Although it depends on the system, PHP typically uses the IEEE 754 double precision format, which will give a maximum relative error due to rounding in the order of 1.11e-16. Non elementary arithmetic operations may give larger errors, and, of course, error propagation must be considered when several operations are compounded.

Additionally, rational numbers that are exactly representable as floating point numbers in base 10, like 0.1 or 0.7, do not have an exact representation as floating point numbers in base 2, which is used internally, no matter the size of the mantissa. Hence, they cannot be converted into their internal binary counterparts without a small loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9999999999999991118....

So never trust floating number results to the last digit, and do not compare floating point numbers directly for equality. If higher precision is necessary, the arbitrary precision math functions and gmp functions are available.

You can use the round or number_format functions to get the desired decimals

Johannn
  • 138
  • 7
  • Thanks for the answer, I read the documentation and I am agree, but one more question... Does it happen randomly?? cause for example 3.98 gets printed well... – Parga Oct 18 '19 at 08:50