[英]Python float to string (scientific notation), in specific format
This is a very specific question, but despite a large number of online resources I cannot seem to find a function which does what I want.这是一个非常具体的问题,但尽管有大量的在线资源,但我似乎无法找到满足我要求的功能。 Assume I have an arbitrary float, say for example "2.".
假设我有一个任意浮点数,例如“2.”。 I want to convert the float into the following format:
我想将浮点数转换为以下格式:
'2.000000E+000'
That is to say: 8 digits before the exponential, and 3 digits (four counting the sign) after the exponential.就是说:指数前8位,指数后3位(四位数)。 There are a few functions which almost (but not quite) yield the desired output.
有一些函数几乎(但不完全)产生所需的输出。 Using
使用
"%.6e" %float(2)
yields the output产生输出
'2.000000e+00'
Which permits me to specify how many digits I have to before the exponential, but not after - it always gives me two digits.这允许我指定在指数之前需要多少位数,而不是之后 - 它总是给我两位数。 The function numpy.format_float_scientific, on the other hand:
另一方面,函数 numpy.format_float_scientific:
numpy.format_float_scientific(2.0000000000, exp_digits=3,precision=6)
Out[30]: '2.e+000'
permits me to specify the number of digits after the exponential, but not before.允许我指定指数之后的位数,而不是之前。 I would like to do both.
我想两者都做。 Do you know how I could achieve this?
你知道我怎么能做到这一点吗?
You can try with unique
to False
for numpy
:您可以尝试使用
unique
to False
for numpy
:
numpy.format_float_scientific(2.0000000000, unique=False, exp_digits=3,precision=6)
Output:输出:
'2.000000e+000'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.