Reputation: 134286
I was looking at the online doc for round()
function in python, which says,
round(number[, ndigits])
....The return value is an integer if called with one argument, otherwise of the same type as number.
So, I wrote the below code.
x = round(32.7)
print 'x is now : ', x
print str(type(x))
print type(x).__name__
Let me explain the last two prints I used in above snippet.
Surprisingly, the current output is
x is now : 33.0
<type 'float'>
float
I was expecting
x is now : 33
<type 'int'>
int
I'm out of ideas. What am I missing?
P.S. For anybody interested, a LIVE VERSION
Upvotes: 0
Views: 203
Reputation: 1
Instead of using round...!try this because round function is to round-off the float values..
However round and type-casting to int both do same work @ background
Hope this helps..!!
x = int(32.7)
print 'x is now :',x
print str(type(x))
print type(x).__name__
or
y=32.7
x=int(y)
print 'x is now :',x
print str(type(x))
print type(x).__name__
Upvotes: 0