I have a number in python like 2.75960192084e-05. I want to display it with less number of numbers after decimal point like 2.759e-05. Note the exponent should remain displayed. Is that possible ?
1 Answer
the Format Specification Mini-Language allows you do do that with:
f = 2.75960192084e-05
print('{:9.3e}'.format(f)) # 2.760e-05
...it will round though.
the 9 means that 9 is the minimal witdh of the resulting string; with 3 places behind the decimal point in exponent notation.
2 Comments
Mark Dickinson
What do you mean by "9 places should be reserved overall"? 9 is just the minimum field width: the formatted value will be padded with spaces if necessary to make up that minimum field width. So the '9' has nothing to do with how many decimal digits appear. (Also, there's no comma in the output.)
hiro protagonist
@MarkDickinson comma vs point... sorry; my locale uses commas normally. yes 'minimum field with' would have been a better choice of words. will rephrase. thanks!