The Symfony\Component\HttpFoundation\JsonResponse
seems to have an odd reaction.
I retrieve a Doctrine "decimal" parameter from my object list and I try to send an array of them with a JsonResponse
. But the decimal value is not the one I expected, the "precision" is really odd, and I'm unable to round it :
My Entity :
...
/**
* @var float Local average Grade
* @ORM\Column(type="decimal", nullable=true, precision=4, scale=2)
*/
private $grade;
...
For this example, I will use only a single result of my data.
When I read it from the database, I can read, as expected : 8.30
Here are the tests I've made :
var_dump(gettype($local->getGrade()));
var_dump($local->getGrade());
var_dump((float) $local->getGrade());
var_dump(round($local->getGrade(), 2));
return new JsonResponse([
'grade' => $local->getGrade(),
'roundedGrade' => round($local->getGrade(), 2)
]);
And here is the result :
string(6) "string"
string(4) "8.10"
float(8.1)
float(8.1)
{
"grade":8.0999999999999996447286321199499070644378662109375,
"roundedGrade":8.0999999999999996447286321199499070644378662109375
}
It doesn't make sense to me. Does anyone have any idea of what is happening here ?
Thank you.