python - 不带加号的科学记数法
问题描述
该'{:e}'.format
函数以“1e+06”的形式打印正值。
是否有其他格式将其显示为“1e6”(负指数显然为“1e-6”)?
或者是否需要自定义格式功能?
解决方案
您可以派生自己的string.Formatter
子类:
import string
class MyFormatter(string.Formatter):
def format_field(self, value, format_spec):
if format_spec == 'm':
return super().format_field(value, 'e').replace('e+', 'e')
else:
return super().format_field(value, format_spec)
fmt = MyFormatter()
v = 1e+06
print(fmt.format('{:e}, {:m}', v, v)) # -> 1.000000e+06, 1.000000e06
推荐阅读
- r - 当数据框中的日期模式匹配时,如何为变量分配随机值?
- algorithm - 我不明白 Givens 旋转矩阵的系数是如何在这里编码的
- c# - 如何移动列表末尾的列表对象,同时移回其他对象?
- python - 如何删除同一字符串列表中包含的其他字符串中包含的字符串?
- node.js - Firebase 从 node.js 后端检测用户登录
- python - 无论如何要挽救这个代码片段以避免内存瓶颈?
- c# - 来自 DelegatingHandler 的“无法多次发送相同的请求消息”错误
- servicestack - 转换失败,在 ServiceStack 中使用 AutoQuery 时
- python - 为什么重新安排时钟间隔会导致图像位置重置?
- amazon-web-services - 该帐户的队列中不能有超过 0 个构建