简体   繁体   English

Python - 必须保留 3 位小数

[英]Python - Mandatory 3 decimal places

I'm stuck on a subject.我被困在一个主题上。 I have a float number with a value of 0.150.我有一个值为 0.150 的浮点数。 Except that in my program in python, my value displays 0.15.除了我在python的程序中,我的值显示0.15。

I want to force the display of 0.150 without converting the number into a character string.我想在不将数字转换为字符串的情况下强制显示 0.150。 Do you know a function, a library that could help me?你知道一个 function 的图书馆吗?

Thanks for your help !谢谢你的帮助 !

EDIT: Here my field Odoo declare:编辑:这里是我的领域 Odoo 声明:

    posX = fields.Float(digits=(4, 3), string="Position X", required=True)

It works fine for the xml field.它适用于 xml 字段。 I have 0.150.我有 0.150。

However, when I use a Python value dictionary, the result displayed is 0.15.但是,当我使用 Python 值字典时,显示的结果是 0.15。 This poses a problem in my program.这在我的程序中造成了问题。 I need 0.150 in float and not in string我需要 0.150 的浮点数而不是字符串

Code to use my value:使用我的价值的代码:

        values += [{
            'name': element.name,
            'posX': element.posX,
            'posY': element.posY,
            'width': element.width,
            'height': element.height,
        }]

Here the resut it's 0.15.这里的结果是 0.15。 Why?为什么?

Float 0,15 or 0,150 are exactly the same number for python. You can only show the extra 0 in Odoo nieuws and on pdf reports but not store it in the python variable.浮点数 0,15 或 0,150 与 python 的数字完全相同。您只能在 Odoo nieuws 和 pdf 报告中显示额外的 0,但不能将其存储在 python 变量中。

For the views and pdf you can use this code:对于视图和 pdf,您可以使用以下代码:

 <t t-esc="'{0:,.3f}'.format(int(values.posX))" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM