- Read Tutorial
- Watch Guide Video
We've seen how if you perform some things such as product cost and I'll put this in a print statement. So if I say print product cost and I multiply this by the quantity then what this is going to do even though quantity is an integer it is going to run this and it's going to convert the final number into a float. So if I hit return you can see that this is a float.
So it performed that conversion for us automatically.
However, if we ever want to do that manually. So if we ever wanted to take for example a float and convert it into an integer or vice versa then we can also do that and that's what we're going to do in this guide and the syntax for this and we're going to go through three of them is to provide the name which is a function and then wrap whatever value we want to convert inside of that. So the very first thing we're going to do is an integer. So I'm going to take a product cost and convert it into an integer even though it's a float right now. I want to convert that and the syntax is going to look like this I'm going to say int(product_cost)
and that we'll convert it for us.
Now I'm going to put this inside of a print statement but you could also put this in a variable and use it however you want. I'm going to say print product cost it started at 88.40 let's see what happens if we run it now. You can see that now it's 88.
Now if I change this to 88.80 and run it again you can see it's still 88.
So it gives us similar behavior to how the floor division computation works where even though 88.8 is closer to 89 all that essentially it's doing is it's just taking the floating-point variable and just throwing it away. And so you have to be cognizant of that if you are converting these values it doesn't round it to the Close's hole number. It simply takes whatever integer value is there and it just keeps that and ignores the rest. So that is something to keep in mind.
Now the next one is. . . let's take our quantity and turn it into a float. So in order to do that the keyword is float and I can say quantity. And now let's see what it does for us if I hit return. You can see 450 gets turned into 450.0 for if you ever need to get that.
Now if you are working with the decimal library which we've worked with than that already that decimal function already converted a float into a decimal.
Let's see how that looks again. So remember to import decimal we say import the decimal library and then say from decimal
import decimal from Decimal
Now what we can do is the same thing except now let's take the product cost again. So let's take that and actually if you see I have a little invalid syntax sense because I got these switched up it should be from decimal import this decimal
from decimal import Decimal
and so now it can use this and we're going to take the product cost and instead of it being 88.80 we're going to convert it into a full proper decimal so let's see what happens when we do that. And you can see what this looks like
and from a mathematical perspective, the way that Python looks at 88.80 is actually with this incredibly complex number of 88.79 and then all of these values after it.
And it is a pretty large number so we've already walked through how to perform that conversion. However, I wanted to give you a nice full list for doing it.
Now, the last one we're going to go through might be a little bit odd if you are not someone who performs a lot of scientific calculations but what you can do is let's do this for the commission rate. So we actually use that value as well. And what we can do is say complex and this is going to give us the scientific notation for commission rate. So if I hit return here you can see
this gives us 0.08 and then gives the full scientific notation in parentheses and this actually because it returns in parens this is giving you a complex object that you can work with. Now you don't typically have to work with the complex data type unless you're performing quite a few scientific type calculations. But I did want to show it to you in case you run into it. The ones that I use the most are probably decimal and int and decimal in the one I use the most out of all of them because it's any time that I want to convert some type of number and have a higher level of precision I bring in the decimal library and you can perform that conversion just like we did right here.
Code
from decimal import Decimal product_cost = 88.80 commission_rate = 0.08 qty = 450 print(int(product_cost)) print(float(qty)) print(Decimal(product_cost)) print(complex(commission_rate))