Below is my output and the value is represented in scaled units like below.
Default value: 2.000000e-02
When ever the value is represented as double(like above), I would like to convert and print like a floating value(0.02) Expected Output Default value: 0.02
Could anyone pls help me with this?
CodePudding user response:
Format strings are the correct choice for this task I think. The .2f
specifies that the number should be converted into fixed-point notation and have exactly two digits after the decimal point. Example:
d = 2.000000e-02
print("{:.2f}".format(d))
Output:
0.02
CodePudding user response:
What you are looking for is called string formatting. %
and .format
are used for this task, pyformat.info has explanation and comparison of boths. In this case you might easily use either, that is to get 2 digits after . you might do
x = 2.000000e-02
print('%.2f' % x) # 0.02
print('{:.2f}'.format(x)) # 0.02
Alternatively you might use so-called f-strings (requires python3.6
or newer) that is
x = 2.000000e-02
print(f'{x:.2f}') # 0.02
Consult your organization style guidelines to detect which one you should use.