python round() 函数

   

Python用于四舍五入的内建函数round() ,它的定义为

lua小数计算精度问题 小数点精确度_git

意思是, 将 小数部分保留到 ndigits 指定的 小数位,也就是 精度保持到 ndigits -1 位; 如果没有 指定 ndigits ,则 精度 保持到 整数位。

   

例如

lua小数计算精度问题 小数点精确度_数位_02

   

直接 round 时, 精确度 为整数, 即 5.0 ; round(n, 5)时,小数位保留 5位,即

   

   

odoo小数位数设置

   

在 odoo中,各业务模块设置了各自的精确度, 通过 模型 decimal.precision 定义 用于不同用途的 精确度【小数位数】, dp 模型 根据 name 返回 小数位数, 默认为 2位

   

常用的精确度用途如下

lua小数计算精度问题 小数点精确度_lua小数计算精度问题_03

   

   

在定义模型的字段时, 通过 Fileds.float() 的属性 digits_compute 设置 小数位数, 例如

   

price_unit = fields.Float(string='Unit Price', required=True, digits=dp.get_precision('Product Price'))

   

通过 dp 类的方法 get_precision 读取 名为 product price 的精确度, 在此例, 读取的值 应是 3, 所以 采购单价 为 3位 小数位

lua小数计算精度问题 小数点精确度_git_04

   

   

   

税金与精确度

   

税金可以设置为 含税价,或者 未含税价

lua小数计算精度问题 小数点精确度_数位_05

   

   

   

odoo 在计算 订单行的 未税金额,含税金额时, 会调用 account.tax 模型的 方法 compute_all() 进行 计算, 计算时 会以 名为 Account 的小数位数设置 作为 round() 方法的小数位数, 在此例 小数位 为 2 , 当 税金为 17% 含税价 5.99, 返回

{

    "taxes": [

        {

            "account_analytic_collected_id": false,

            "account_analytic_paid_id": false,

            "account_collected_id": 49,

            "account_paid_id": 49,

            "amount": 0.87,

            "base_code_id": false,

            "base_sign": 1.0,

            "id": 2,

            "name": "\u589e\u503c\u7a0e17%\u8fdb\u9879\u7a0e",

            "price_unit": 5.119658119658119,

            "ref_base_code_id": false,

            "ref_base_sign": 1.0,

            "ref_tax_code_id": false,

            "ref_tax_sign": 1.0,

            "sequence": 1,

            "tax_code_id": false,

            "tax_sign": 1.0,

            "todo": 0

        }

    ],

    "total": 5.12,

    "total_included": 5.99

}

   

算式 为

round(5.99/1.17, 2) = 5.12

   

   

在 compute_all() 方法中 预置了 个选项 round_globally,

lua小数计算精度问题 小数点精确度_lua小数计算精度问题_06

   

启用 此选项时, 小数位数 增加 5位, 同样对于 5.99 /1.17 的处理,则更精确, 结果为