I'd always get 240.0 and not 240.00
Asked
Active
Viewed 39 times
-3
-
Welcome to the StackOverflow. You'll benefit from reading [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – EDG956 Jul 07 '22 at 12:25
1 Answers
0
This is very basic python knowledge and there are extensive results if you are searching for it on your favorite search engine. There are different ways to format floats in Python. Here are two:
toFormat = 240.0
formatted = "{:.2f}".format(toFormat)
formatted2 = '%.2f' % toFormat
print(formatted)
print(formatted2)
Gives this output:
240.00
240.00
Edit:
As I understood the question is that you have a sum and a tax-percentage. sum = 2000
and tax = 0.12
and you want to print this in a format of 2 decimal points?
Here is an example of a solution, but there are many ways to solve this question.
tax = 0.12
sum = 2000
print("Tax: %.2f" % (sum*tax))
print("Tax: {:.2f}".format(sum*tax))
Output:
Tax: 240.00
Tax: 240.00

Sabsa
- 191
- 9
-
got it thank you! but like for example if i write print( "tax:" 2000*.12), the result's 240.0 , so where do i insert the formatted one so the output's gonna be 240.00? – ruri Jul 07 '22 at 12:36
-
@ruri you need to provide more code for me to be able to answer that in a good way. What is your code snippet? Please edit your question to include your code and expected output and what you have done to try to solve the problem. But I gave it a try in my answer – Sabsa Jul 07 '22 at 12:38
-