I am trying to round an equation of two float numbers but it outputs without the decimals, it just rounds it to the nearest number for example 21.3 to 21. When I put ", 2" which should set it to round to the nearest 10th.
New code:
def add(op1,op2):
result = int(round(op1 + op2, -1))
print("")
print ("Output: %d + %d = %d" % (op1, op2, result))
Output:
Output: 10 + 10 = 20
Output:
Output: 10.3 + 10.9 = 21.0
Code:
def add(op1,op2):
result = int(round(op1 + op2, 1))
print("")
print ("Output: %0.1f + %0.1f = %0.1f" % (op1, op2, result))
Here's how to round a number to the nearest tenth:
>>> round(21.3331, 1)
21.3
Here's how to print out a floating point number with one decimal point:
>>> print "%.1f" % (21.3331,)
21.3
Note that if you do it using %d
it will get printed as rounded to the nearest integer:
>>> print "%d" % (21.3331,)
21
See the String Formatting Operations docs for more on how the %
formatting works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With