Python print all digits of float
For a scientific application I need to output very precise numbers, so I have to print 15 significant figures. There are already questions on this topic here, but they all concern with truncating the digits, not printing more. Show I realized that the So the question is, how can I easily print a variable amount of signifcant figures of my floats, where I need to display more than 10? asked Feb 23, 2012 at 15:26
3
Let:
For 3 significant figures:
For 3 decimal places:
See the "presentation types for floating point and decimal" table at the bottom of this section for any additional requirements provided by answered Oct 19, 2018 at 20:26
Mateen UlhaqMateen Ulhaq 22.2k16 gold badges86 silver badges127 bronze badges 4 You
could use the string formatting operator
or
In new code, the latter is the preferred style, although the former is still widely used. For more info, see the documentation. answered Feb 23, 2012 at 15:27
NPENPE 471k104 gold badges922 silver badges997 bronze badges 3 Thought the original question wanted to format n significant figures, not n decimal points. So a custom function might be required until some more native built-in types are on offer? So you'll want something like:
answered Oct 27, 2013 at 2:25
To display N significant figures (not decimal places) you use the "g" format:
See format spec for details on precision:
answered Jun 3, 2019 at 0:10
quantoidquantoid 5304 silver badges16 bronze badges 1 Use these two common print idioms for formatting. Its a matter of personal taste on which is better.
Personally I think the first is more compact and the 2nd is more explicit. Format has more features when working with multiple vals. answered Feb 23, 2012 at 16:05
Matt AlcockMatt Alcock 11.9k13 gold badges44 silver badges60 bronze badges 1 You could use this function I wrote, it seems to be working fine and it's quite simple!:
Some tests:
I hope this helps you ;) answered Aug 2, 2014 at 17:24
1 How do I print a full float number in Python?To print float values with two decimal places in Python, use the str. format() with “{:. 2f}” as str.
How do I print .2f in Python?In Python, to print 2 decimal places we will use str.format() with “{:.2f}” as string and float as a number. Call print and it will print the float with 2 decimal places.
What is %s %d %F in Python?The formatting using % is similar to that of 'printf' in C programming language. %d – integer %f – float %s – string %x – hexadecimal %o – octal The below example describes the use of formatting using % in Python.
|