Symfony – Rounded decimal in edit form Symfony2 + Doctrine2

decimaldoctrine-ormsymfony

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.

Best Answer

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?