[英]Is there a way not to format number in Jinja2 templates using Ansible?
Jinja2 seems to be shortening number when used in template with Ansible, I have this in: role/x/vars/main.yml: var_a: 0.010101010101010101
Jinja2 在 Ansible 模板中使用时似乎在缩短数字,我在:role/x/vars/main.yml:
var_a: 0.010101010101010101
In role/x/templates/a.txt.j2: my_var_a={{ var_a }}
在role/x/templates/a.txt.j2:
my_var_a={{ var_a }}
After running it, I get the number in a.txt shortened by two(missing 01 at the end): my_var_a=0.0101010101010101
运行后,我得到a.txt 中的数字缩短了 2(最后缺少 01):
my_var_a=0.0101010101010101
Is there a way not to format it?有没有办法不格式化?
This is not a problem of Jinja but of the representation of floating point numbers in Python (and C).这不是 Jinja 的问题,而是 Python(和 C)中浮点数表示的问题。
I tried this in python:我在 python 中试过这个:
>>> var_a= 0.010101010101010101
>>> print(var_a)
0.0101010101010101
So indeed the number is truncated.所以确实数字被截断了。 I advise you to read this explanation about floating point arithmetic .
我建议您阅读有关浮点运算的说明。
Another example that shows the limitations:另一个显示限制的示例:
>>> 0.1 + 0.2
0.30000000000000004
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.