Decimal conversion of python Scientific notation
1 Question
When float converts str, the str() method can be used to convert conveniently. However, numbers with too long decimal places will be converted to the Scientific notation format by default, such as:
>print(str(0.000001))
1e-06
In practical applications, this form of data is often not what we want
The problem to be solved in this paper is to convert a two-dimensional array with Scientific notation values in str format into decimal form.
input data( str)
-999. 0. 0. 0. 0.
0. 0. 0. 0. 0.
5.00e-02 5.20e+01 2.55e+02 2.55e+02 2.55e+02
6.00e-02 6.90e+01 1.95e+02 2.53e+02 2.55e+02
8.00e-02 1.29e+02 2.21e+02 9.00e+01 2.55e+02
1.00e-01 2.55e+02 1.87e+02 1.50e+01 2.55e+02
1.50e-01 2.55e+02 6.90e+01 1.00e+00 2.55e+02
2.00e-01 2.55e+02 1.50e+02 1.50e+01 2.55e+02
0.4 255. 87. 15. 255.
expected output( str)
-999. 0. 0. 0. 0.
0. 0. 0. 0. 0.
0.05 52. 255. 255. 255.
0.06 69. 195. 253. 255.
0.08 129. 221. 90. 255.
0.1 255. 187. 15. 255.
0.15 255. 69. 1. 255.
0.2 255. 150. 15. 255.
0.4 255. 87. 15. 255.
II Solution
There are several methods to find:
1. Format string f-string syntax
https://docs.python.org/3.4/library/string.html#format -Specification mini language.
> number = 0.0000001
> f"Number: {number}"
'Number: 1e-07'
> f"Number: {number:f}"
'Number: 0.000000'
> f"Number: {number:.10f}"
'Number: 0.0000001000'
Can only be used for a single floating-point operation.
2. numpy Cancel Scientific notation
Numpy Cancels Scientific notation
Suppress; True indicates the cancellation of scientific notation.
>import numpy as np
>np.set_printoptions(suppress=True, threshold=np.nan)
3. Pandas Cancel Scientific notation
>import pandas as pd
>pd.set_option('display.float_format',lambda x : '%.3f' % x)
III Complete code
def table_process(table):
np.set_printoptions(suppress=True)
table_arr = np.fromstring(table, sep='n')
table_arr = color_arr.reshape((table_arr.size//5, 5))
new_table = str(table_arr)
new_table = re.sub('\[', '', new_table)
new_table = re.sub('\]', '', new_table)
return new_table