0

So, I've been trying to round a number but it always gives me a result which I know is wrong, for example:

355 / 60

While this should be presenting me with a float around 5.9 etc. , the output on my machine is just 5, and if I specify float (355 / 60) the output is 5.0

I noticed this was very wrong, so I tried the same thing in trinket.io and the output was correct.

What is wrong with my IDLE, I suspect it's an issue which has to do with me using python 2.7 and trinket.io using 3, but my output shouldn't be so wrongly rounded, right?

Jerry Stratton
  • 3,287
  • 1
  • 22
  • 30
Henrique
  • 45
  • 4
  • 2
    In python 2, `/` is an integer division, so `355 / 60` is already truncated to `5` and `round(5) == 5`. – Holt Dec 02 '15 at 11:00
  • have a look here http://ubuntuforums.org/showthread.php?t=947270 – Markon Dec 02 '15 at 11:01
  • If you divide two ints in python 2, you get an int. If you cast the result to a float, you get a float equal to the int you already worked out. However, if one of the operands is a float, you get float division. E.g. `355.0/60` – khelwood Dec 02 '15 at 11:01

1 Answers1

1

335 and 60 are Integer numbers. When division is done on integers the result would be an integer. To make the result a floating point you can add .0 to one of the operands or cast one using float() function.

Amir
  • 820
  • 9
  • 30