更改数字格式 更改数字值

发布于 2025-01-09 19:20:11 字数 524 浏览 2 评论 0原文

我有一个“salaire”对象,显示在树枝一侧。

 {{ salaire.getMontant_euros_revalorise() }}

注意:salaire.getMontant_euros_revalorise() 是一个浮点数。

它显示:

20,685.05

这是美国显示,我想将其更改为欧洲显示,这意味着逗号是一个点,点是逗号。

所以我尝试使用这个:

 {{ salaire.getMontant_euros_revalorise()|number_format(2, ",", ".") }}

不幸的是,它改变了数字的值。它显示:

20,00

所以从两万六百八十五点零五我们到二十。

我不知道为什么会这样做,但有人知道如何正确更改点和逗号符号吗?最后有:

20.685,05

I have a "salaire" object that I display on the twig side.

 {{ salaire.getMontant_euros_revalorise() }}

Note: salaire.getMontant_euros_revalorise() is a float.

it displays this :

20,685.05

It is an american display, I would like to change it to a european display meaning that the comma is a point and the point a comma.

So i tried to use this :

 {{ salaire.getMontant_euros_revalorise()|number_format(2, ",", ".") }}

Unfortunatly, it changes the value of the number. It displays this:

20,00

So from twenty thousand six hundred eighty five point zero five we go to twenty.

I dont know why it does that but does somebody know how to chnage correctly the point and comma signs ? having at the end :

20.685,05

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我恋#小黄人 2025-01-16 19:20:11

如果 getMontant_euros_revalorise 实际上返回 20,685.05 而没有任何修改,那么您存储的是一个字符串,而不是浮点数。因此,解决此问题的最佳方法是确保将数据存储为实数/浮点而不是字符串,或者确定代码已经在何处修改该值。

如果这是不可能的,那么您需要替换 < code>comma 在应用过滤器之前。

{{ foo|replace({',': '',})|number_format(2, ',', '.') }}

演示

If getMontant_euros_revalorise actually returns 20,685.05 without any modification, then you have stored a string, not a float. So the best way to solve this, is to be sure you store the data as a real number/float rather than a string or determine where your code is already modifying the value

If this is not possible, then you would need to replace the comma before applying the filter.

{{ foo|replace({',': '',})|number_format(2, ',', '.') }}

demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文