Rounded decimal in Symfony2 + Doctrine2 edit form

How to set decimal precision and scale within a symfony2 object? I defined latitude in longitude fields using the decimal data type in my entity as follows:

/** * @ORM\Column(type="decimal", precision="10", scale="6", nullable=true) */ protected $latitude; 

When saving a new object, it saves the decimal value correctly (I can see 52.406374 in phpMyAdmin). However, when the editing form is displayed, the number is rounded to 52,406 , and after updating it is stored in mysql as 52.406000. Maybe something is wrong with my Doctrine configuration, but I don't know what.

Why is latitude and longitude rounded?

Found a solution:

The Symfony Form component displays the decimal form field as type = "text". Using:

 ->add('latitude', 'number', array( 'precision' => 6, )) 

It is displayed correctly. The thread from the comment does not describe my problem. I think they have already fixed the error with precision "convert to float". When I showed the latitude in the branch template, it showed the correct (not rounded) value.

+6
source share
3 answers

Perhaps the problem is not with your mapping doctrine. See this reference documentation for NumberType .

Is that what you use? You may have to change the accuracy.

+2
source

How to set the precision and scaling of the decimal type of Symfony:

Define it in your entity, for example:

 protected $mycolumn; /** * @ORM\Column(type="decimal", precision=14, scale=8, nullable=true) */ 

This sets the data type in the MySQL database:

 mycolumn decimal(14,8) 

What does scale and precision mean when specifying the type of decimal field in Doctrine 2?

+4
source

In Symfony3.2 you should use the scale:

 $builder->add('lat', NumberType::class, array('scale' => 8)); 

Indicates how many decimal places will be allowed as long as the field rounds the passed value (via rounding_mode). For example, if the scale is set to 2, the supplied value of 20.123 will be rounded to, for example, 20.12 (depending on your rounding_mode).

+2
source

Source: https://habr.com/ru/post/917786/


All Articles