Symfony2 + Doctrine2编辑形式的十进制舍入 [英] Rounded decimal in edit form Symfony2 + Doctrine2

查看:92
本文介绍了Symfony2 + Doctrine2编辑形式的十进制舍入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在symfony2实体中设置小数精度和比例?我使用这样的实体中的十进制数据类型在经度字段中定义了纬度:

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;

持久化新对象时,会正确保存十进制(我可以在phpMyAdmin中看到52.406374)。然而,当显示编辑表单时,数字四舍五入为 52,406 ,更新后将其存储在mysql中为52.406000。可能我的Doctrine配置有问题,但我不知道是什么。

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?

找到一个解决方案:

Symfony的Form组件将decimal形式字段渲染为type =text。使用时:

Symfony's Form component renders decimal form field as type="text". When using:

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

正确呈现。来自评论的帖子没有描述我的问题。
我猜他们已经修正了'转换为浮动'精度的错误。
当我在twig模板中显示纬度时,它显示正确(不是四舍五入)值。

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.

推荐答案

也许是问题不是由于你的教义映射。
请查看 NumberType 的参考文档。

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.

这篇关于Symfony2 + Doctrine2编辑形式的十进制舍入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆