【问题标题】:Print numbers in terms of engineering units in Python [duplicate]在Python中以工程单位打印数字[重复]
【发布时间】:2012-10-20 05:05:39
【问题描述】:

可能重复:
Print number in engineering format

如何以 3 的倍数以科学计数法打印数字?例如:

1.5e4      --> 15e3
1.27e-8    --> 12.7e-9
2.9855e-11 --> 29.855e-9

我正在寻找类似于计算器上的ENG 按钮的功能。是否有 Python 包可以做到这一点?

【问题讨论】:

    标签: python printing numbers format


    【解决方案1】:

    似乎还没有这样的功能(至少在 Python 2.7 中),请参阅:http://bugs.python.org/issue8060http://bytes.com/topic/python/answers/616948-string-formatting-engineering-notation 页面上,我找到了以下解决方案(我个人不太喜欢,但似乎可行):

    import math
    
    for exponent in xrange(-10, 11):
        flt = 1.23 * math.pow(10, exponent)
        l = math.log10(flt)
        if l < 0:
            l = l - 3
        p3 = int(l / 3) * 3
        multiplier = flt / pow(10, p3)
        print '%e =%fe%d' % (flt, multiplier, p3)
    

    只需根据您的需要进行调整即可。

    编辑: 也请看这里Print number in engineering format

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-30
      • 1970-01-01
      • 2011-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多