Python - 指数中的位数 [英] Python - number of digits in exponent
本文介绍了Python - 指数中的位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以设置用于打印浮点数的指数的位数?我想将它设置为3.
目前,
f = 0.0000870927939438012
>>> %.14e%f
'8.70927939438012e-05'
>>> %0.14e%f
'8.709279e-005'
我想要print是:
'8.70927939438012e-005'
解决方案
有没有办法控制,最好的办法是为此编写一个函数
def eformat(f,prec ,exp_digits):
s =%。* e%(prec,f)
尾数,exp = s.split('e')
# ($,$,$,$,$,$,$,$,$,$)
print eformat(1.0000870927939438012e5,14,3)
print eformat(1.1e123,4,4)
print eformat(1.1e-123,4,4)
$ b $输出:
8.70927939438012e -005
1.00008709279394e + 005
1.1000e + 0123
1.1000e-0123
Is it possible to set the number of digits to be used for printing the exponent of a floating-point number? I want to set it to 3.
Currently,
f = 0.0000870927939438012 >>> "%.14e"%f '8.70927939438012e-05' >>> "%0.14e"%f '8.709279e-005'
What I want to print is:
'8.70927939438012e-005'
解决方案There is a no way to control that, best way is to write a function for this e.g.
def eformat(f, prec, exp_digits): s = "%.*e"%(prec, f) mantissa, exp = s.split('e') # add 1 to digits as 1 is taken by sign +/- return "%se%+0*d"%(mantissa, exp_digits+1, int(exp)) print eformat(0.0000870927939438012, 14, 3) print eformat(1.0000870927939438012e5, 14, 3) print eformat(1.1e123, 4, 4) print eformat(1.1e-123, 4, 4)
Output:
8.70927939438012e-005 1.00008709279394e+005 1.1000e+0123 1.1000e-0123
这篇关于Python - 指数中的位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文