-3

I wanna round after decimal point. I know that the function round does it but when the number has only zeros after decimal point I also wanna keep the amount of zeros that I want, becouse python leaves only 1 zero.

round(54.983839,2) == 54.98
round(54.0, 2) == 54.0    # and here I want 54.00
Erik André
  • 528
  • 4
  • 13
Py Ja
  • 1
  • 1
    have a look at https://stackoverflow.com/questions/455612/limiting-floats-to-two-decimal-points – g_uint Dec 02 '20 at 10:41
  • https://stackoverflow.com/questions/20457038/how-to-round-to-2-decimals-with-python – Matthijs990 Dec 02 '20 at 10:41
  • 3
    It sounds like what you really want to do is *format* the number with two decimal digits for display purposes. `round` is an actual mathematical calculation, and it can't give you `54.00` instead of `54.0` because *they are not different things*. – Karl Knechtel Dec 02 '20 at 10:42
  • Well, both rounding and format, and yes 54.00 = 54.0 but for Competitive programming purposes I needed exactly 2 digits after decimal point not matter what type of digits. – Py Ja Dec 02 '20 at 10:57

2 Answers2

0

You can use format():

print(format(54.0, ".2f"))

The output will be:

54.00
lorenzozane
  • 1,214
  • 1
  • 5
  • 15
0

Use string formatting in python: "%.2f" % (54.0).