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
How to format floating numbers using inbuilt methods in Python?
How do I limit a float to only show 2 decimal places?
How do I display infinity or nan values properly?
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))
a = 5 / float(2.7)
b = int("5") / int(2.7)
print(a, b)
why are we getting two different numbers?
The way I look at it is if "b" can print a float number (because it has a decimal in it) why is it giving a different number than converting the 2.7 as a float?
I am on day 2 of python learning so I apologize if I am not using the proper terminology.