【问题标题】:UDF in xlwings calculating with defined precision using Python Decimal or mpmathxlwings 中的 UDF 使用 Python Decimal 或 mpmath 以定义的精度计算
【发布时间】:2022-12-15 10:27:41
【问题描述】:

我正在尝试在 xlwings 中使用 UDF,并且我想达到所需的精度,因为 Python Decimal 和 mpmath 库都允许这样做。我正在使用以下函数计算 2 ^ 0.5:

@xw.func
def fce_2(x, y):
    import decimal
    from decimal import Decimal
    decimal.getcontext().prec = 100
    z = Decimal(x)** Decimal(y)
      
    return z

这将返回 1,4142

@xw.func
def fce_1(x, y):
    from mpmath import mp
    mp.dps = 100
    z = mp.mpf(x)** mp.mpf(y)
      
    return z

返回 1,414213562 它们都没有返回所需的小数位数,应该是 100。

【问题讨论】:

    标签: python decimal xlwings mpmath


    【解决方案1】:

    Excel 单元格中的数字始终存储为 64 位双精度浮点数,具有大约 15 位有效数字。应该可以将更多数字显示为字符串,使用return str(z)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-17
      • 2014-11-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-19
      • 1970-01-01
      • 1970-01-01
      • 2016-10-13
      相关资源
      最近更新 更多