Convert floating point number to a certain precision, and then copy to string
Solution 1
With Python < 3 (e.g. 2.6 [see comments] or 2.7), there are two ways to do so.
# Option one
older_method_string = "%.9f" % numvar
# Option two
newer_method_string = "{:.9f}".format(numvar)
But note that for Python versions above 3 (e.g. 3.2 or 3.3), option two is preferred.
For more information on option two, I suggest this link on string formatting from the Python documentation.
And for more information on option one, this link will suffice and has info on the various flags.
Python 3.6 (officially released in December of 2016), added the f
string literal, see more information here, which extends the str.format
method (use of curly braces such that f"{numvar:.9f}"
solves the original problem), that is,
# Option 3 (versions 3.6 and higher)
newest_method_string = f"{numvar:.9f}"
solves the problem. Check out @Or-Duan's answer for more info, but this method is fast.
Solution 2
Python 3.6
Just to make it clear, you can use f-string formatting. This has almost the same syntax as the format
method, but make it a bit nicer.
Example:
print(f'{numvar:.9f}')
More reading about the new f string:
- What's new in Python 3.6 (same link as above)
- PEP official documentation
- Python official documentation
- Really good blog post - talks about performance too
Here is a diagram of the execution times of the various tested methods (from last link above):
Solution 3
Using round
:
>>> numvar = 135.12345678910
>>> str(round(numvar, 9))
'135.123456789'
Solution 4
In case the precision is not known until runtime, this other formatting option is useful:
>>> n = 9
>>> '%.*f' % (n, numvar)
'135.123456789'
Solution 5
It's not print that does the formatting, It's a property of strings, so you can just use
newstring = "%.9f" % numvar
Related videos on Youtube
pauliwago
Updated on July 02, 2020Comments
-
pauliwago almost 4 years
I have a floating point number, say
135.12345678910
. I want to concatenate that value to a string, but only want135.123456789
. With print, I can easily do this by doing something like:print "%.9f" % numvar
with
numvar
being my original number. Is there an easy way to do this?-
michael_s about 11 years% exactly does that - % is not part of the print function but of string - see Python docs
-
-
Zaren about 11 yearsOr use the new style formatting. valueString ="{:.9f}".format(number)
-
Caltor over 10 yearsoption two should be
newer_method_string = "{:.9f}".format(numvar)
- note the required:
to separate the field and the formatting. I have tested this on 2.7.5 anyway. -
ttq over 7 yearsFor python 2.6 option two should be
newer_method_string = "{0:.9f}".format(numvar)
-- note the required 0 for the field_name for this older version. -
Peter Mortensen almost 6 yearsWhat is up with
.
vs.,
? And how is it relevant for pauliwago's question? -
nivk over 5 yearsIf you'd prefer to use
.format
-method, note that this can also be done by nesting arguments like so:'{:.{n}f}'.format(numvar,n=n)
. -
Cameron Tacklind over 3 years@PeterMortensen It's a confusing european standard to use a list separator (aka comma:
,
) as a decimal separator I think -
Minh Nghĩa over 3 yearsIs there a syntax for specifying accuracy other than 9?
-
Or Duan over 3 years@MinhNghĩa just change the
9
to any number you want then -
Minh Nghĩa over 3 yearsI mean arbitrary accuracy, like
n
-
Or Duan over 3 yearsDo you mean a dynamic number that is being calculated in run time? If so, you will have to use
round
-
Charlie Parker about 3 yearsI want significant figures not just truncating blindly t according to
{v.:3f}
. How do I get significant figures? -
jyalim about 3 years@CharlieParker I'd recommend using exponential notation, for example
{:12.5e}
. There's more in this subsection of the documentation (docs.python.org/2/library/…).