round(number[, ndigits]):
>>> round(0.3223322, 2)
0.32
Note that you’ll probably still want to use a certain precision of string formatting when producing output due to floating point imprecision.
Depending on what you’re trying to achieve, it might be appropriate to use the Decimal type:
>>> from decimal import Decimal
>>> round(Decimal(0.3223322), 2)
Decimal('0.32')
which does its math in (surprise!) decimal instead of binary and therefore doesn’t suffer any issues with decimal rounding (except initially if you’re trying to create it from a float).
Answer from Ry- on Stack Overflowround(number[, ndigits]):
>>> round(0.3223322, 2)
0.32
Note that you’ll probably still want to use a certain precision of string formatting when producing output due to floating point imprecision.
Depending on what you’re trying to achieve, it might be appropriate to use the Decimal type:
>>> from decimal import Decimal
>>> round(Decimal(0.3223322), 2)
Decimal('0.32')
which does its math in (surprise!) decimal instead of binary and therefore doesn’t suffer any issues with decimal rounding (except initially if you’re trying to create it from a float).
This is not as nice, but I'm always a fan of:
f=float(int(100*f))/100
Far from the best way to do it, but it's one I use often.
How to turn a float number like 293.4662543 into 293.47 in python? - Stack Overflow
Python: Print floating point numbers with two digits after decimal - Stack Overflow
How to use the float() command to create 2 decimal places instead of one in a product of 2 numbers [the product is dollar amount]
How to remove decimals in float?
Videos
Example:
hourlyRate = 20 hoursLabor = 1.6
I want the answer to show “32.00” instead of “32” or “32.0”.
What I have so far produces the number 32.0:
totalPay = float(hourlyRate * hoursLabor)
print(totalPay)
I’m obviously very new at this. Just getting some beginner’s practice :)
From The Floating-Point Guide's Python cheat sheet:
"%.2f" % 1.2399 # returns "1.24"
"%.3f" % 1.2399 # returns "1.240"
"%.2f" % 1.2 # returns "1.20"
Using round() is the wrong thing to do, because floats are binary fractions which cannot represent decimal digits accurately.
If you need to do calculations with decimal digits, use the Decimal type in the decimal module.
If you want a number, use the round() function:
>>> round(12.3456, 2)
12.35
(but +1 for Michael's answer re. the Decimal type.)
If you want a string:
>>> print "%.2f" % 12.34567
12.35
You can use print(f'{total:.2f}) to print the first 2 numbers after the float point. Or, if you want, you can change the quantity of numbers changing the ".2f"in the sentence.
Or, you can use the round() function. Try this: round(total, 2) The 2 number represents how many number will appear after the dot.
I'm assuming you're having the issue when a float is entered as price ($99.99).
You could use str(round(total, 2)) to round your numbers to the 2nd decimal place.
price = float(input("Price $"))
tax = .05
salestax = (price*tax)
total = ((price*tax)+ price)
print("Sales tax $" + str(round(salestax, 2)))
print("Total $" + str(round(total, 2)))
outputs to:
Price $45
Sales tax $2.25
Total $47.25
You may run into rounding errors where the sales tax will only list 1 decimal place like $5.0. But should work for most prices.