How do I set the decimal precision and scale within the symfony2 entity? I've defined latitude in longitude fields using decimal data type in my entity like this:
/**
* @ORM\Column(type="decimal", precision="10", scale="6", nullable=true)
*/
protected $latitude;
When persisting new object it saves decimal correctly (I can see 52.406374 in phpMyAdmin). However when displaying edit form the number is rounded to 52,406 and after updating it is stored in mysql as 52.406000. Probably something is wrong with my Doctrine configuration, but I don't know what.
Why is the latitude and longitude getting rounded?
Found a solution:
Symfony's Form component renders decimal form field as type="text". When using:
->add('latitude', 'number', array(
'precision' => 6,
))
It is rendered properly. The thread from comment doesn't describe my problem. I guess they have already fixed bug with 'convert to float' precision. When I was displaying latitude in twig template it was showing proper (not rounded) value.
How to set the symfony decimal type precision and scale:
Set it in your entity, like this:
protected $mycolumn;
/**
* @ORM\Column(type="decimal", precision=14, scale=8, nullable=true)
*/
This sets the datatype in the MySQL database:
mycolumn decimal(14,8)
What do scale and precision mean when specifying a decimal field type in Doctrine 2?
Maybe the problem is not due to your doctrine mapping. Look at this reference documentation for NumberType.
It is what you use? You probably have to change the precision.
In Symfony3.2 you should use scale:
$builder->add('lat', NumberType::class, array('scale' => 8));
This specifies how many decimals will be allowed until the field rounds the submitted value (via rounding_mode). For example, if scale is set to 2, a submitted value of 20.123 will be rounded to, for example, 20.12 (depending on your rounding_mode).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With