0

I am trying to round this integer in Python, but for some reason it rounds it to 0.0 if the variable is equal to 51 when it should round to 50 essentially.

This is the function code:

def cm(centimeter):
    """Centimeter to Meters Converter!"""
    result = round(centimeter / 100, -1)
    print ("%d centimeters is the same as %d meters." % (centimeter, result))
    print (result)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user2839430
  • 123
  • 1
  • 2
  • 8

6 Answers6

4

First, centimeter / 100 is 0 for all values between 0 and 99 inclusive. / is, by default, an integer division. Divide by 100.0 to force this to be a floating point division.

Second, 51 / 100.0 is 0.51. To -1 digits of precision, this is 0. I'm not sure why you think it should be 50, after you've already divided it by 100. I assume you mean 0.5. In which case you want:

result = round(centimeter / 100.0, 1)

Or maybe you just meant to divide by 100 after rounding:

result = round(centimeter, -1) / 100.0
kindall
  • 178,883
  • 35
  • 278
  • 309
1

If you are using Python 2.x and are passing an integer value for centimeter, you should make sure the division is using floating point before rounding. So just change 100 to 100. and it should work.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bogatron
  • 18,639
  • 6
  • 53
  • 47
1

Change this line:

result = round(centimeter / 100, -1)

to

result = round(centimeter / 100., 2)  # 2 decimal places
                               ^  ^

Console session:

>>> round(51/100, 0)
0.0
>>> round(51/100.0, 0)
1.0
>>> round(51/100.0, -1)
0.0
>>> round(51/100.0, 2)
0.51
>>> round(51/100.0, 1)
0.5
Games Brainiac
  • 80,178
  • 33
  • 141
  • 199
1

If you want it to be .5, change your function to the following

def cm(centimeter):
    """Centimeter to Meters Converter!"""
    result = round(centimeter / 100., 1)
    print ("%d centimeters is the same as %f meters." % (centimeter, result))
    print (result)

Your issue was that you were using the ndigits parameter of the built-in function round incorrectly. ndigits is that second argument you were passing in which was -1. In built-in round, values are rounded to the closest multiple of 10 to the power minus ndigits. This meant it was rounding it to the closest multiple of 10^1 in your code.

51 / 100. in Python gives you 0.51 so when you round 0.51 to the closest multiple of 10^1, you obviously were getting 0.0.

Another issue is that you were trying to print your number of meters as a signed decimal value using the string formatting conversion %d. If you want to see the exact decimal value of result, you need to use the option for floating point decimal format which is %f. For more information on this stuff, check out the string formatting page in the docs.

Shashank
  • 13,713
  • 5
  • 37
  • 63
  • I guess I didn't clarify it too well I'm sorry for that, lets say if centimeters is 51 I want it to output the number as 0.5 but atm it rounds it to 0. I don't want it to round to 1 or 0, I want it to round to the closest 10th like 0.5,0.6 etc.. – user2839430 Oct 02 '13 at 16:39
  • @user2839430 edited post, the first part will make your number 0.5000. – Shashank Oct 02 '13 at 16:43
0

centimeter / 100 does integer division and will return 0 for values of centimeter below 100. You want centimeter / 100. which will return a float that is passed to round.

Greg Whittier
  • 3,105
  • 1
  • 19
  • 14
0

Try:

round(centimeter, -1)/100.

This operation should give 0.5, which is (including your rounding) the correct answer.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
PhillipD
  • 1,797
  • 1
  • 13
  • 23