O
O
Oleg Petrov2018-10-11 08:55:04
Python
Oleg Petrov, 2018-10-11 08:55:04

How to convert floating point number 6.737e-0.5 to number like 0.000023233?

How to convert floating point number 6.737e-0.5 to number like 0.000023233?
I display the price of an asset and it is constantly displayed in the format of a floating point number, but I need ordinary numbers with any number of digits. The search turned up nothing.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eternalfire, 2018-10-11
@Smeilz1

You can use the f format specifier and specify the number of decimal digits:

>>> '{:.10f}'.format(1e-10)
'0.0000000001'

The default number of characters is 6, so:
>>> '{:f}'.format(1e-6)
'0.000001'
>>> '{:f}'.format(1e-7)
'0.000000'

If you need to remove zeros after the decimal point, you can use the function rstrip:
>>> '{:f}'.format(1.1)
'1.100000'
>>> '{:f}'.format(1.1).rstrip('0')
'1.1'

By default, when converting to a string using str (or repr), python returns the shortest possible representation for the given float, which can be exponential notation.
>>> '{:.16f}'.format(1.1)
'1.1000000000000001'
>>> 1.1
1.1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question