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
priceUSD. Add the following code beneath
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
%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
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