+ 2
How to round to 2 decimal places in python
So I have completed the python beginner program and I'm trying to solve all the pre-set problems in the community area. I'm working through the easy ones and have got stuck on Kaleidoscope. The problem I'm having is in adding discounts and tax to an integer but python returning with a float with 7+ decimal places. I thought there was something wrong g with my code for ages but then worked out it's to do with Python working with guestimating fractions as decimals rather than precision decimal maths. Is there a way to get python to round a float to 2 decimal places, as it's meant to be displaying currency?
8 odpowiedzi
+ 1
Only round the end result. Not the intermediate results.
+ 1
use the round() function to round a float
+ 1
First of all you have to import math on the top of your code, the you can make use of the round() method
Eg:
maths = 3 * 10969.88
maths = round(maths, 2)
print(maths)
OUTPUT: 32909.64
🤓
+ 1
Yes, you can use the round() function in Python to round a float to a specific number of decimal places. In your case, you can use round(number, 2) to round the number to 2 decimal places.
Here's an example:
number = 10.1234567
rounded_number = round(number, 2)
print(rounded_number) # Output: 10.12
Alternatively, you can use the format() function to format the number as a string with 2 decimal places:
number = 10.1234567
formatted_number = "{:.2f}".format(number)
print(formatted_number) # Output: 10.12
Note that the round() function returns a float, while the format() function returns a string.
In your case, you can use the round() function to round the result of your calculation to 2 decimal places, like this:
total_cost = round((price * quantity) * (1 - discount) * (1 + tax), 2)
This will give you the total cost with 2 decimal places, which is suitable for displaying currency.
I hope my answer can help you .
0
So I have tried
round(x, 2)
but it is just turning everything into whole integers.
This is my full code:
num =int(input())
kal = 5
def discount(x):
if x > 1 : (x*0.9)
round (x, 2)
return x
dis_num = discount(num)
total_kal = (dis_num*kal)
def tax(x):
(x*1.07)
round(x, 2)
return x
final_price = tax(total_kal)
print (final_price)
I was also wondering would it work to work in pennies and then divide by 100 so that it shows in pounds/dollars at the end.
For example saying that Kaleidoscope s cost 500 instead of 5. And then at the end before I print the final number divide by 100 so that python turns 535 into 5.35.
0
Thank you,
I spoke to someone else and they saw another error in my code. I have amended it to this:
num =int(input())
kal = 500
def discount(x):
if x > 1 : x = (x*0.9)
return x
dis_num = discount(num)
total_kal = (dis_num*kal)
def tax(x):
x = (x*1.07)
return x
final_price = (tax(total_kal)/100)
y = round(final_price, 2)
print (y)
0
In python there is no such function. to get it done, we can use an f-string like this:
val = 42.1
print(f'{val:.2f}') # for more details have a look at the python docs
result is: 42.10
An answer proposed by Lothar