Well I would atleast clean it up as follows:
print "%.2f kg = %.2f lb = %.2f gal = %.2f l" % (var1, var2, var3, var4)
Answer from Frank Krueger on Stack OverflowWell I would atleast clean it up as follows:
print "%.2f kg = %.2f lb = %.2f gal = %.2f l" % (var1, var2, var3, var4)
Format String Syntax.
https://docs.python.org/3/library/string.html#formatstrings
from math import pi
var1, var2, var3, var4 = pi, pi*2, pi*3, pi*4
'{:0.2f}, kg={:0.2f}, lb={:0.2f}, gal={:0.2f}'.format(var1, var2, var3, var4)
The output would be:
'3.14, kg=6.28, lb=9.42, gal=12.57'
python - How to print a float variable using the % formatting? - Stack Overflow
How can I control the number of decimals when printing a float?
Printing float objects
printing - Python print a float with a given number of digits - Stack Overflow
Videos
There are several options to evaluate expressions and print them as a string in python.
There are already some good answers, but here are some explicit examples and links to the documentation.
Formatted string literals (f-strings)
f-strings allow you to input expressions which are evaluated at run-time. In the f strings expressions are encased by curly brackets.
As an example:
x = 42.222222222
print(f'My value is: {x}')
prints My value is: 42.222222222.
and with specifying the format:
x = 42.222222222
print(f'My value is: {x:.2f}')
prints My value is: 42.22.
Str formatting method
Strings have a built-in .format() method where you can specify replacement fields with curly brackets.
As an example:
x = 42.222222222
print('My value is: {}'.format(x))
prints My value is: 42.222222222.
and with string formatting:
x = 42.222222222
print('My value is: {:.2f}'.format(x))
prints My value is: 42.22.
String formatting operator
String formatting operator
As an example:
x = 42.222222222
print('My value is: %' % x)
prints My value is: 42.222222222.
and with string formatting:
x = 42.222222222
print('My value is: %.2f' % x)
prints My value is: 42.22.
See @Felk answer for some more qualitive descriptions of the different methods.
As you try print float number, use %f instead of %d. This code will print the number to 4 decimal places:
result_2 = 4.523529411764706
statement_2a = "Your text contains an average length of %.4f letter(s) per words." % result_2
print(result_2)
print(statement_2a)
I want to control how many digits will be printed after the decimal point when printing a float.
The function will take a, b and p as inputs, divide a/b, and I need it to show p number of decimals.
For example, if a/b = 123.456789, then:
If p = 2, then output 123.46 If p = 3, then output 123.457 If p = 10, then output 123.4567890000
The formating method for both integer and decimal is
>>> '{:06.2f}'.format(3.141592653589793)
'003.14'
the part before the . (6 here) denotes the total length of padding (including the .), and after the . (2fhere) denotes digits after decimal point.
Hope it helps. checkout the link.
You can try to do something like this
print('{:.6}'.format(val))
As no one has added it, it should be noted that going forward from Python 2.6+ the recommended way to do string formating is with format, to get ready for Python 3+.
print ["{0:0.2f}".format(i) for i in a]
The new string formating syntax is not hard to use, and yet is quite powerfull.
I though that may be pprint could have something, but I haven't found anything.
A more permanent solution is to subclass float:
>>> class prettyfloat(float):
def __repr__(self):
return "%0.2f" % self
>>> x
[1.290192, 3.0002, 22.119199999999999, 3.4110999999999998]
>>> x = map(prettyfloat, x)
>>> x
[1.29, 3.00, 22.12, 3.41]
>>> y = x[2]
>>> y
22.12
The problem with subclassing float is that it breaks code that's explicitly looking for a variable's type. But so far as I can tell, that's the only problem with it. And a simple x = map(float, x) undoes the conversion to prettyfloat.
Tragically, you can't just monkey-patch float.__repr__, because float's immutable.
If you don't want to subclass float, but don't mind defining a function, map(f, x) is a lot more concise than [f(n) for n in x]