在python中以指数格式删除前导0 [英] remove leading 0 in exponential format in python

查看:34
本文介绍了在python中以指数格式删除前导0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Python 3.1 中有以下代码

I have the following code in Python 3.1

"{0:.6E}".format(9.0387681E-8)

它给出了一个 9.038768E-08 的字符串,但我想要字符串 9.038768E-8E-08 的前导 0> 删除.我应该怎么做?

Which gives a string of 9.038768E-08, but I want the string 9.038768E-8 with the leading 0 of E-08 removed. How should I go about this?

推荐答案

你可以这样做

"{0:.6E}".format(9.0387681E-8).replace("E-0", "E-")

或者像 JBernardo 建议的那样更好

or better as JBernardo suggests

format(9.0387681E-8, '.6E').replace("E-0", "E-")

如果你有大数字,你也需要替换E+0

You need to do a replace for E+0 as well if you have big numbers

这篇关于在python中以指数格式删除前导0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆