-1

I have this, but it does not work

number = int(input("Please give the number"))
print("Formatted Number: " + "{:.2f}".format(number));
print(number)
CristiFati
  • 38,250
  • 9
  • 50
  • 87
Linn
  • 5
  • 2
  • 2
    What exactly doesn't work? What's the expected output – CristiFati Feb 09 '20 at 09:07
  • Does this answer your question? [Limiting floats to two decimal points](https://stackoverflow.com/questions/455612/limiting-floats-to-two-decimal-points) – Shubham Shaswat Feb 09 '20 at 09:09
  • Does this answer your question? [How can I format a decimal to always show 2 decimal places?](https://stackoverflow.com/questions/1995615/how-can-i-format-a-decimal-to-always-show-2-decimal-places) – theletz Feb 09 '20 at 09:34

3 Answers3

1

You are getting error with this code because you are trying to convert the float value to int while taking input. If you want to take a float input from user then round the value as you want and turn it into a string value then you can use the code provided below:

number = float(input("Please give the number"))  # take input as float

print("Formatted Number: " + str(round(number,2)))  # round upto 2 and convert to string  

Please let me know if this is what you are looking for or not.

Shahriar Rahman Zahin
  • 624
  • 1
  • 11
  • 23
0

Following syntax that you mentioned

print("Formatted Number: " + "{:.2f}".format(number));

will work but you cant round int number.

Remove "int" from number = int(input("Please give the number"))

and replace it with "float" number = float(input("Please give the number"))

0

In place of "int" you can use "eval" more about eval

  number = eval(input("Please give the number"))
  print("Formatted Number: " + "{:.2f}".format(number));
  print(number)
Welcome_back
  • 1,245
  • 12
  • 18