Types and Type Conversion
Start course
Difficulty
Beginner
Duration
3h 39m
Students
7
Ratings
5/5
starstarstarstarstar
Description

This course begins by downloading Android studio - make sure you watch the appropriate video depending on whether you're on Mac or Windows. Then, we'll take a tour off the Android Studio interface and see how apps are put together. You'll learn about text views, buttons, and images to build a user interface for our app, and we'll also write some code to make our apps interactive.

Then we'll move onto a practical project in which we make a temperature converter app, which converts temperatures from Fahrenheit to Celsius. You'll be able to follow, building the app, and then running it on your system.

Intended Audience

This course is intended for beginners to Android app development or anyone who wants to master coding in Kotlin.

Prerequisites

Since this is a beginner level course, there are no requirements, but any previous experience with coding would be beneficial.

Transcript

Hello, and welcome back. Hope you were able to complete the task I had assigned at the end of the last video. So, this is what it looks like. I have a Log.i for information on both of my methods. The first one toCelsius sets the tag to toCelsius with the message of button clicked. And the second one, toFahrenheit sets the tag to toFahrenheit and the message to button clicked. So, when I actually go ahead and click on a button here, I'll click on toCelsius. You see it says toCelsius and button clicked. And then if I click on toFahrenheit to fahrenheit: button clicked. Perfect. Okay, moving on. We'll now work with Kotlin number types in this video, which will set us up for the math that we have to do within these two methods. 

Because remember, we're getting the view type of edit text over here. So, whatever we input in our app right here, we're going to receive it as editable, which we're going to convert to string. And editable is what we get when we reference the text property of the view. And then in prior videos, you've seen that we've been using .toString to convert that to string and then display it, either in our logs or in the form of a message to the user using Toasts. Now, since we have to do some math with it, we actually have to convert the first editable type that we get to string, and then the string to a number type before we can do the math. 

So, let's pull up a browser window in our Kotlin playground and look at some other types like numbers in Kotlin. Here we go. Here is the playground and if you pull up another browser tab and type in Kotlin number types, this is the page that pops up and you can look at numbers and the various types that are available. And two prominent types you see here are Float and Double, and we're going to use double for our app. If you look over here, the type of double allows us to have a digit, decimal point, and a lot of numbers after the decimal point. A very prominent type for numbers is integer, and integers are whole numbers. They don't have decimals and digits after the decimals. 

So unfortunately, they're not going to work for us here because with temperatures and conversion you're looking at math and you're looking at decimal digits. And you can read through this if you want to understand number types and some other things about convergence, run some sample code, and more, but I'm going to go back to my playground and work on the conversion. So, I have this string variable set up with this number 75.6, but it 's within double quotes. So, this is actually a string this is not a number. And here, I have the two formulas that I have pasted in: Celsius to Fahrenheit conversion formula and then I have Fahrenheit to Celsius conversion formula. This is the math that we're going to need to do. And if you want to look this up, you can go to Google and type in, Celsius to Fahrenheit formula. And the first link is usually a good one. 

Right here, this has the Celsius to Fahrenheit formula, and if you scroll down, you can look at Fahrenheit to Celsius formula here as well. So, I basically copy this formula down here. So, let's do the first conversion, Celsius to Fahrenheit, all right? So, I'm going to say val myConvertedValue and then I'll enter in this formula right here. This is simply temp_in_celsius * 9/5 + 32. And I'm going to get rid of the parentheses, I don't need it. So, my converted value is this, but temp_in_celsius, I don't have this variable anywhere. So, this is going to give me an error, myVariable is my input text, right? So right here, I will say myInputText, and then let me print out the result. So, println(myConvertedValue). And if I run this, I'm going to get an error. 

And I'm getting all these errors because myInputText right here is of type string and I cannot use a string to perform this calculation. I need to convert this to a number that I can use. And as we saw here, I can convert it to a double, which is what we decided to use, right? Double. So I can do that simply by saying .toDouble(). If I do that, then this number becomes a double, then it performs a calculation, and then I can print this value out. So, 75.6 Celsius to Fahrenheit should be a pretty large number. Let's run it and see what it is. There you go, 168 degrees Fahrenheit. It's pretty hot. We're going to test it out using a different number, let's say 0. 0 degrees Celsius should give me about 32 degrees Fahrenheit. So, if I run it, there you go, 32. Great. 

Now, I'll go back to 75.6. And now, let's try out the second formula, temp in Fahrenheit -32*5/9 command C, and I'll put this here, command V, all right? And then, we already know that I need to change this to myInputText.toDouble(). And why do we have this extra parentheses around this expression before we get to the multiplied 5*9? That's because if we didn't, then due to precedence of mathematical operators, the multiplication between 32 and 5 divided by 9 would take place first, and then the result we would get is incorrect. So, in order to override the default precedence of operators that's there, in our case we want this subtraction to happen before this multiplication. You can put the expression within parentheses. 

So, that overrides the precedence and whatever you have inside the parentheses takes place first and then that result then gets multiplied by 5*9, all right? So, let's try this out. So, this is conversion from Fahrenheit to Celsius. If I run it, there you go. 24.22 degrees Celsius, that's what 75.6 Fahrenheit is equal to. But this number, right? Eventually, I want to convert this back to string and then display it to the user in the form of a Toast in my app. Now, if I converted this number to a string, it would be this entire thing, 24.222222 whatever. And that's not really that user friendly, right? Too many decimal digits. I don't need all this. I simply need two digits after the decimal. That would look good. So, in order to do that, I can't simply use the .toString which we've been using so far. 

But we can use a method called format that's provided by the string class to specify how many decimal digits we want. Let's try it out. I'll say val toPrint and I'll set it to this two decimal digit string. So, to do that, I can say String, which is the class name .format, which is the method name that I want to use, and then within parentheses, first you provide how many digits you want and that's in the form of within quotes, you put in %. and then the number of digits. So, let's say if we wanted four digits, I would say 4 and then f, then comma. The second one is what you want to convert. So, what I want to convert is myConvertedValue which is a double. 

So, it will not only convert this string, but it will convert it with four decimal digits. So, if I run this now, whoops, I think I have a mistake. I'm supposed to print toPrint not myConvertedValue, which is the old one. So, toPrint. Check it out, four decimal digits. Now, if I wanted two instead of four, I can simply do 2 over here. There you go, 24.22. Perfect. So, this is the process we're going to use. We're going to get our edit text, convert it to string, take the string, convert it to double, perform our calculation and then before presenting it back to the user, we'll convert back to string using this formatting method, all right? Great. So, now that we know this process, I'm going to leave it at that in this video, and in the next video, we'll take what we've learned here and apply it to our app. I hope you're excited, I'll see you there.

 

About the Author

Mashrur is a full-time programming instructor specializing in programming fundamentals, web application development, machine learning and cyber security. He has been a technology professional for over a decade and has degrees in Computer Science and Economics. His niche is building comprehensive career focused technology courses for students entering new, complex, and challenging fields in today's technology space.

Covered Topics