1

I have a problem with round and inserting into database. Doing the following and trying to insert $y into Mongo will insert it with multiple decimals (similar to what you see from var_export below).

How can I fix this, as I need to have exactly 2 decimals.

<?php

$y = round(348.78 + 348.78 * 19/100, 2 );

var_dump($y);
var_export ($y);

This is only to display the difference on how it gets inserted into Mongo.

float(415.05)
415.05000000000001
Alexandru R
  • 8,560
  • 16
  • 64
  • 98

1 Answers1

0

Float datatype is not a good option to represent high precision numbers. Decimal is made for that.

See here how (and why) to use decimal numbers with mongoDB

Eriks Klotins
  • 4,042
  • 1
  • 12
  • 26