且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用Python中的工程单位打印数字

更新时间:2023-02-11 16:17:35

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

It appears there isn't such a feature yet (at least in Python 2.7), see: http://bugs.python.org/issue8060 On the page http://bytes.com/topic/python/answers/616948-string-formatting-engineering-notation I found the following solution (which I personally don't like that much, but seems to work):

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)

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

Just adapt it according to your needs.

也请点击此处以工程格式打印编号