0

I have a function that returns a float number which can have from 0 to up to 8 zeros after the decimal point.

I want to format the float in non-scientific notation and display only up to 4 non-zero digits following the zeros after the decimal point.

For example:

1.42 should be formatted as 1.42  
1.4205152 should be formatted as 1.4205  
1.42e-2 should be formatted as 0.0142  
1.452e-4 should be formatted as 0.0001452  
1.49315e-5 should be formatted as 0.00001493  

etc

Is there a format specifier in python to do so, or writing a custom function is the only solution?

2
  • Have you looked at the format function? Commented May 24, 2020 at 19:15
  • Yes; I did. But doesn't the format function only takes argument for a fixed number of digits after decimal point? i.e. {0.xf}.format will give me x digits after decimal point. Commented May 24, 2020 at 19:39

1 Answer 1

2

There is no built-in support for this. That is, there is no format specifier or conversion function that does this directly. You will need to write your own code to do the formatting.

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

1 Comment

Thank you for that piece of information. I'll proceed to write one without worrying about reinventing the wheel.

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.