【问题标题】:How scientific notation inclusion is decided implicitly in python?python中如何隐含地决定科学记数法包含?
【发布时间】:2021-03-08 10:30:37
【问题描述】:

python中科学计数法包含在计算结果中的标准是什么?

d1 = Decimal('500')
d2 = Decimal('0.25')
print d1/d2

这给出了结果 2.0E+3

如果我在 500 的小数点后包含 2 个零,即,

d1 = Decimal('500.00')
d2 = Decimal('0.25')
print d1/d2

这给出了结果 2000

另外如何确定结果中没有科学记数法?

【问题讨论】:

    标签: python python-2.7 scientific-notation


    【解决方案1】:

    请参阅官方文档中的FAQ

    问。一些十进制值总是以指数表示法打印。有没有办法获得非指数表示?

    A.对于某些值,指数表示法是表示系数中有效位数的唯一方法。例如,将 5.0E+3 表示为 5000 保持值不变,但不能显示原件的两位意义。

    如果应用程序不关心跟踪重要性,很容易删除指数和尾随零,失去重要性,但保持值不变:

    def remove_exponent(d):
        return d.quantize(Decimal(1)) if d == d.to_integral() else d.normalize()
    
    >>> remove_exponent(Decimal('5E+3'))
    Decimal('5000')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-13
      • 1970-01-01
      • 1970-01-01
      • 2018-11-22
      • 2022-06-12
      • 2018-02-05
      • 2019-04-15
      相关资源
      最近更新 更多