0

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 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.)
@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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.