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
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.
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
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
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