Hello welcome to antother Python tutorial and we’re going to talk about converting number types. You know we got integers and floating point numbers and what happens if we want to convert them well there's a built-in function or actually two building functions for that.
So the first thing I want to do is we discussed this in brief earlier in a previous tutorial how to get the type that a certain object is.
And we do that
>>> type(6)
<class 'int'>
>>>
Now we want to know what the type of object numbers are sixes, so get returned is gonna say classes and interger, so it's an integer. Let's try this again.
>>> type(6.0)
<class 'float'>
>>>
We'll see what that object is it's a floating-point number. So type gives us the ability to know what type of object it is, so we could also do something like
>>> type({})
<class 'dict'>
>>>
and it'll tell us dict for dictionary.
>>> type(True)
<class 'bool'>
>>>
We could do type let's do true return it's a boolean. So this gives us the ability to check the types but it doesn't give us an ability to change them. So to convert that to a say we ask a user for a number say how many times did you go to the store last week and they put in three, well it's gonna come back as a string but we want it as an integer so we can do some math with it. So let's say we want to convert a string to an integer so we go
>>> int("3")
3
>>>
and we'll say the user gave us a string of three we close out the parentheses we return it returns an integer, pretty cool right.
So what happens if the user gives us a floating-point number or six point seven or our programming business a floating-point number
>>> int(6.7)
6
>>>
we use int to get that number convert it to an integer now it always rounds down so if we put int it's gonna round down to six.
Now say well I wanted to round up
>>> round(6.7)
7
>>>
You can use round and do six point seven it close it out and you're gonna get seven alright that's another way to work around it it's gonna give you a seven as an integer.
>>> int(round(6.7))
7
>>>
So that's one way to do it or you could actually do in switch I don't know why you would do it this way but just for knowledge sake you can actually do it this way you know return seven still okay.
Now say let's see how about converting it to a floating point number, so say the user gave us three again
float("3")
3.0
so we do float and it's in a string three and we want it in a floating point number so we get we use float built-in function and it converts it to a three.
What about an integer
>>> float(6)
6.0
>>>
Just float six it’s gonna return six point zero, alright. So float converts the number or the string to a floating point number. Int converts a string or a floating point number to an integer.
Now what happens if the user gives us a floating point number in the string and we want to convert it to an integer
>>> int("7.5")
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
int("7.5")
ValueError: invalid literal for int() with base 10: '7.5'
>>>
so if we do this is not gonna work. Now we can't convert a string that contains a floating point number, it's not a floating point number but say that wakes it looks like a floating point number and we convert them to a floating point number. This is not this is a string object okay, so if we go int 7.5 we're gonna get an error it says invalid literal for int with base 10.
So basically saying we need in the string we need it to be an integer and that's the only way you can convert.
Well what happens if I want convert my users input to an integer, so let's say the user gave us the floating-point number 7.5 first thing we're gonna do is
>>> int(float("7.5"))
7
>>>
what we're doing here is we're gonna convert it to a floating-point number. Which is gonna give 7.5 then we're gonna convert it to an integer so we hit return there we go alright, or we could have used round to get the floating-point number if we don't give rounded argument
>>> round(float("7.5"))
8
>>>
this way it return we get eight. So here we go into all these roles rounds down the 7th and 8th all these rounds up. so here's proof that int gonna always round down and round will round the proper way it's supposed to unless the fraction doesn't meet up like we've talked about a million times before.
Alright so uh anything else I want to show you oh yeah when we go back to int here I just remember I said this is a string not a floating-point number let's just take a look at that real quick
>>> type("7.5")
<class 'str'>
>>>
it's a string alright, it's based of whatever is indicated on the outside. So like I said earlier I said this was a string
it is actually string.
Now what we're doing here
We're converting it to a floating-point number because float can handle a string that looks like this all right int cannot. So we have to convert it to a float which is 7.5 and then convert it to an integer which is 7 alright and it always your on sale and it you're always rounds down.
If you guys have any questions leave a comment on below.
You should throw in some examples of negative numbers. Depending on the programming language, negative numbers may round differently.
I've never programmed in Python, but I think it rounds everything toward negative infinity, if memory serves, but no doubt, you would know better than I. :)
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit