python指数e,Python - 指数中的位数

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值