utils.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:cerberus-core 作者: ovh 项目源码 文件源码
def float_to_decimal(f):
    """
    Convert a floating point number to a Decimal with
    no loss of information. Intended for Python 2.6 where
    casting float to Decimal does not work.
    """
    n, d = f.as_integer_ratio()
    numerator, denominator = Decimal(n), Decimal(d)
    ctx = Context(prec=60)
    result = ctx.divide(numerator, denominator)
    while ctx.flags[Inexact]:
        ctx.flags[Inexact] = False
        ctx.prec *= 2
        result = ctx.divide(numerator, denominator)
    return result
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号