简体   繁体   English

没有加号的科学计数法

[英]Scientific notation without plus sign

The '{:e}'.format function prints positive values in the form "1e+06". '{:e}'.format函数以“ 1e + 06”的形式打印正值。

Is there some other format which instead displays it as "1e6" (and negative exponents obviously as "1e-6")? 还有其他格式将其显示为“ 1e6”(负指数显然显示为“ 1e-6”)吗?

Or would a custom format function be necessary? 还是需要自定义格式功能?

You could derive your own string.Formatter subclass: 您可以派生自己的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM