0

I am using MongoDB with PHP MongoDB driver manager. In my database, I have stored float values.

Save data type: Double.

Save value    : Return values
1.0           : 1.000000000000548542525
1.23          : 1.235421620001245224544
1.2314        : 1.231447542145214265854

I cant round them in my PHP code, because I don't know it has 2 values after float point (.) or 4 values.

Inserting data is perfect, I check in MongoDB compass. Also on localhost, it is working ok. but return different float values on the live server

Zain Farooq
  • 2,956
  • 3
  • 20
  • 42
asad app
  • 321
  • 1
  • 2
  • 14
  • check mongodb and drive version both in server and local – Shibon Apr 30 '19 at 12:26
  • On local i have Windows, and on live i have centos 7 WHM – asad app Apr 30 '19 at 12:27
  • If you want precision for floating point values you need to use a Decimal type instead of a Double. [Doubles cannot accurately represent all real numbers](https://en.wikipedia.org/wiki/Floating-point_arithmetic#Accuracy_problems), which leads to rounding errors and a loss of precision. If precision isn't important you could perhaps round these to a consistent number of digits after the decimal point, but if accuracy is more important a Decimal representation will be best. – Stennie Apr 30 '19 at 13:31

0 Answers0