## Introduction to Decimal Number Variables: Android Studio Crash Course (Free Tutorial)

Have you ever needed to display numbers with decimal places whilst coding a program? If so, this is the article for you. If you are a beginner and want to learn more, check out our FREE 30-minute introductory course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

In this example, we’ll be looking at an online store that is converting the prices it displays for its items from CAD to USD. To follow along in Android Studio, go into Project view. Then go to app >> java >> (topmost) com.example.zebra.demo >> MainActivity.

In order to implement decimal numbers in Android Studio, we use the keyword Double. Let’s name two double variables priceCAD and priceUSD. Add the following code beneath setContentView(R.layout.activity_main);:

Double price CAD, priceUSD;

To convert prices, we need to have a conversion rate. On a new line above the one in which we declared double variables, declare a constant by using the keyword for constants: final. As well, specify the data type Double. Name this constant CAD_TO_USD, and set it equal to 0.76, a sample conversion rate.

final Double CAD_TO_USD = 0.76;

Back with our double variables, set priceCAD to equal 9.55. You can do negative numbers, but this example requires positive numbers. Note that if you want a price of 9 you have to type “9.0”. If you just put “9”, you’ll get an error. The emulator will find an integer when it wants a double variable, which requires decimal places. Also, make priceUSD equal to priceCAD + CAD_TO_USD.<

Double priceCAD = 9.55, priceUSD = priceCAD * CAD_TO_USD;

Furthermore, let’s create the code for a message to be displayed on the screen. Below the line with our double variables, type in “Toast”, select “Create a new Toast”, and hit Enter. Android Studio will auto-complete the following code with the Toast function’s parameters:

Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();

The string we want to be displayed on the screen goes into the quotation marks of the Toast function. Let’s code it as "The price in USD is " + priceUSD.

To test our code, run the emulator. You should see the text “The price in USD is 7.258000000000001”. Woah! This price contains 15 decimals. The online store probably doesn’t want to show the price of its items with those many.

To format the price to two decimal places, add the following before the string in our Toast function: String.format(). Enclose the string in the parentheses of that code.

As well, inside those parentheses, add the parameter Locale.getDefault(), in the beginning. Then replace + with a comma. Put a placeholder for the first decimal that appears after the comma. After "The price in USD is", write %.2f. %f stands for floating point number. The .2 specifies that we want only two decimal places to be shown.

If you want to specify the decimal places for more numbers, add more %fs separated by and. Separate the numbers with commas, such as priceUSD, 2.45. The value of priceUSD will be placed in f. If we have another one called 2.45, it will place that number in the second f.

For now, we’ll just use one placeholder. If you run the emulator again, you will see the price displayed with two decimal places: “The price is 7.26”.

For more FREE tutorials, check out our 30-minute beginners course on coding: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

## Introduction to Converting Strings to Decimals: Android Studio Crash Course (Free Tutorial)

Ready to get an edge on the competition? This article is about converting strings to decimals with numeric values stored in strings. As such, you’ll be able to simplify your code and reduce your chance of error.

Want to learn coding from scratch but don’t know where to start? Check out our FREE 30-minute introductory course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

To follow along with this example in Android Studio, go into Project view. Then go to app > java > (topmost) com.example.zebra.demo > MainActivity.

To begin, declare a string variable phoneName, and give it the value "Nexus5X". Do so by typing in the following code beneath setContentView(R.layout.activity_main);:

String phoneName = "Nexus 5X";

Before the semi-colon, initialize another string phoneDisplay, and give it the value "5.2". As per convention, separate the two strings with a comma.

Note that 5.2 is a string variable, and you can’t do operations on strings. Therefore, if you want to convert 5.2, which is in inches, into centimeters, you can’t multiply it by a conversion rate.

Instead, you can convert that value into a new variable. On the next line, type Double, the key word used to declare a decimal variable. Declare the double variable phoneDisplayIn, and make it equal to Double.parseDouble().

With this code, the emulator will read whatever parameter we pass as a string inside the parentheses as a double. Pass the string phoneDisplay in the parentheses.

Note that the numeric value of the phone display will be in inches. For those of you using the metric system, we’ll also need the phone display in centimeters. On the same line, after a comma, create a new variable phoneDisplayCm. End the line with a semi-colon. Your line of code should look like this:

Double phoneDisplayIn = Double.parseDouble(phoneDisplay), phoneDisplayCm;

The value of phoneDisplayCm will be equal to the value of phoneDisplayIn multiplied by the conversion rate from inches to centimeters. Let’s create a constant for this conversion rate. On a new line above the double variables, use the key word final, and create the double constant IN_TO_CM. Assign it the value 2.54, the number of centimeters in an inch.

final Double IN_TO_CM = 2.54;

Now that you have your conversion rate constant, set phoneDisplayCm equal to phoneDisplayIn times the value of IN_TO_CM.

Double phoneDisplayIn = Double.parseDouble(phoneDisplay), phoneDisplayCm = phoneDisplayIn * IN_TO_CM;

There we go! We have variables for the display in centimeters and in inches. By converting the type from a string to a double variable, we eliminate our chance of error.

Next we want to create the code for a message to be displayed on the screen. On a new line below your double variables, type in “Toast”, select “Create a new Toast”, and hit Enter. Android Studio will auto-complete the following code:

Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();

For the displayed message, we could concatenate all of our values and strings into the quotation marks of the Toast function. However, it is much neater to create a new string containing the message we want to be shown. Then, we can just reference the string’s name in the Toast function. By doing so, we refrain from overfilling the Toast function with too many parameters.

Replace the quotation marks in the Toast function with message. In a new line above the Toast function, create the new string message. Give it the value phoneName + " has a screen display of " + phoneDisplayCm + " cm." Your line should look like so:

String message = phoneName + " has a screen display of " + phoneDisplayCm + " cm."

Run the emulator to see the values that show up on the screen. By zooming in to the display, you will see the message “Nexus 5X has a screen display of 13.208 cm.” Perfect! This is exactly what we were expecting. Now you know how about type conversion from string to decimal and how to make these operations with numeric values that are stored in strings.

For more FREE tutorials, check out our 30-minute beginners course on coding: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

## Introduction to Constants: Android Studio Crash Course (Free Tutorial)

Ready to get ahead of the game? Today you’ll be learning something that will give you an edge in computer science. Although constants aren’t often used by programmers, they increase the readability of programs. In this tutorial, we will use a simple math example to see constants’ usefulness in action. If you are a beginner and want to start with the basics of coding for FREE, check out our 30-minute introductory course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

To follow along with this example in Android Studio, go into Project view. Then go to app > java > (topmost) com.example.zebra.demo > MainActivity. Feel free to hide Project view.

To understand the purpose of constants in programming, let’s look at a simple math problem: if the radius of a circle is 2.54 inches, find the circumference of the circle in centimeters.

To start off, initialize a double variable using the keyword Double. Double variables allow our numbers to have decimal places. Call it radiusInInches, and assign it the value 2.54. Do so by typing the following code beneath setContentView(R.layout.activity_main);:

Double radiusInInches = 2.54;

The next variable to declare is one for the circumference (perimeter) of the circle. Let’s call it perimeterInCentimeters. Assign it the value of the mathematical formula for the circumference of a circle: 2 * pi * r = 2 * 3.14 * 2.54. Note that we also have to multiply the value by 2.54 because that’s approximately the number of centimeters in one inch.

Double perimeterInCentimeters = 2 * 3.14 * 2.54 * 2.54;

Although our problem is technically solved, our code isn’t quite easy to understand. Later on, when you read this code again, you may wonder what the numbers being multiplied mean. Since our radius is 2.54 and that number appears twice in our formula, you might be tempted to say that we are actually using the formula for the AREA of a circle (pi * r^2).

This is where constants come in. They make code easier to understand. The key word final is used to declare a constant. Note that we can declare constants from any data type desired. In this case, we’ll declare a double constant because our number contains decimal places. On a line between your two declared variables, declare a double constant named CENTIMETERS_IN_ONE_INCH. Set it equal to 2.54.

final Double CENTIMETERS_IN_ONE_INCH = 2.54;

Notice the difference in the naming conventions for constants and variables. As you can see, the names of variables begin with a lowercase letter, and all the other words in the name begin with an uppercase letter. For the names of constants, all letters are in uppercase, and the words are separated with underscores.

Now let’s replace the numbers in perimeterInCentimeters with more meaningful names. Change the first 2.54 in perimeterInCentimeters to radiusInInches. Replace the remaining 2.54 in that line with CENTIMETERS_IN_ONE_INCH.

Hooray! The code looks much easier to understand. However, there’s one more problem we have to tackle: the “3.14”. There’s a predefined constant in Android Studio called Math.PI. It is accurate to 15 decimal places of Pi, which minimizes round-off errors. You can replace 3.14 with this constant for better precision.

That’s that! Now the programs you create will be much more easily read. This is helpful because code is much more often read than written. If you want to improve your coding skills even further, check out our FREE 30-minute beginners course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

## Introduction to Comparing Strings: Android Studio Crash Course (Free Tutorial)

If there’s one thing you should learn about coding, it’s how to compare strings. Strings are pretty important because they’re the messages that get displayed on computer screens. With this article, you’ll learn how to use Android Studio to determine whether or not certain strings are equal.

If you want to learn the basics of coding for FREE, check out our 30-minute introductory course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

To follow along with this example in Android Studio, go into Project view. Then go to app > java > (topmost) com.example.zebra.demo > MainActivity. Feel free to hide Project view.

First off, let’s declare 2 strings, car1 and car2, and name them “Toyota” and “Honda”. As well, we want to specify that there’s a message we want to be displayed on the screen. To do so, add the following code beneath setContentView(R.layout.activity_main);:

String car1 = "Toyota", car2 = "Honda", message;

Right away, create If/Else Statements:

if(){

}
else{

}

In order to compare two strings, we have to use a method called “equals”. Type the following into the parentheses of your If Statement: car1.equals(). In the parentheses of THIS code, write car2 as a parameter.

If the two strings are equal (in other words, contain the same values), let’s have the message “Strings are EQUAL.” appear on the screen. The code of your If Statement should look like this:

if(){
message = "Numbers are EQUAL.";
}

If the numbers AREN’T equal, let’s have the message “Strings are NOT EQUAL.” show up by coding that in our Else Statement.

In order for the message to display on the screen, below your Else Statement, type in “Toast”, select “Create a new Toast”, and hit Enter. Android Studio will auto-complete the following code:

Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();

Replace the double quotation marks with message. The screen will display the content in message.

If you run the emulator and zoom in, you will see “Strings are NOT EQUAL.” appear on the screen because “Toyota” isn’t the same as “Honda”. To see the message “Strings are EQUAL.” give car1 and car2 the same value.

Well done! You now know how to compare strings in Android Studio. If you want to learn even more coding for FREE, check out our 30-minute beginners course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

## Introduction to Comparing Integers: Android Studio Crash Course (Free Tutorial)

Welcome to a key concept in Android Studio: comparing integers. No matter what coding language you’re using, integers are one of the most common ways to represent a number. If you want to learn the basics of coding for FREE, check out our 30-minute introductory course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

We are going to compare integers in order to determine whether or not they’re equal. To follow along with this example in Android Studio, go into Project view. Then go to app > java > (topmost) com.example.zebra.demo > MainActivity.

Beneath setContentView(R.layout.activity_main);, declare integers number1 and number2, and assign them the values 9 and -18:

int number1 = 9, number2 = -18;

Create If/Else Statements:

if(){

}
else{

}

Next, we have to create a message that will be displayed on the screen. First, initialize an empty string in case the emulator doesn’t run the If/Else Statements.

String message = "";

Because we’re comparing numbers 1 and 2 for equality, type the following into the parentheses of your If Statement: number1 == number2.

As you may have learned from our article on Boolean Variables, one equals sign assigns a value to a variable. A double equals sign compares two numbers and checks whether or not they’re equal.

As part of our If Statement, let’s have the message “Numbers are EQUAL.” display on the screen.

if(number1 == number2){
message = "Numbers are EQUAL.";
}

On the other hand, if the numbers aren’t equal, let’s have the message “Numbers are NOT EQUAL.” show up by adding that in the Else Statement. Note that all of your statements must end in semi-colons.

Furthermore, we have to create a Toast message on the screen. Below your Else Statement, type in “Toast”, select “Create a new Toast”, and hit Enter. Android Studio will auto-complete the following code:

Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();

Replace the double quotation marks with message. The screen will display the content in message.

By running the emulator and zooming in, you will see the string “Numbers are NOT EQUAL.” appear on the screen because 9 doesn’t equal -18. To see the message “Numbers are EQUAL.” set numbers 1 and 2 to the same value.

If you want to learn even more coding for FREE, check out our 30-minute beginners course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes