Bummer! This is just a preview. You need to be signed in with a Pro account to view the entire video.
Start a free Basic trial
to watch this video
In this video we'll see how to format numbers differently based on the locale of our users!
Related Links
-
0:00
The app looks great in Spanish.
-
0:02
But if we pick a color, like purple, then we see the price as $19.99.
-
0:09
And while we do want to display the price in dollars,
-
0:12
this isn't the correct way to format a number for a Spanish audience.
-
0:15
In Spain, and most of Europe and
-
0:17
South America, instead of using a dot as a decimal mark, they use a comma.
-
0:22
So this should really be 19,99.
-
0:25
One way we could do this, would be to set up a switch statement based on the locale
-
0:30
of the user, and then manually swap out dots for commas, and vice versa.
-
0:35
But that would be a ton of work, and
-
0:36
requires to know the preference of each country we want to support.
-
0:41
Luckily, Android's got an easy way for
-
0:43
us to deal with this in the number format class.
-
0:46
Let's open up our main activity, And
-
0:50
at the bottom of onCreate, let's create a variable for our price,
-
0:56
float price, and let's set it equal to 19.99f.
-
1:02
Then, on the next line let's create a new NumberFormat variable,
-
1:08
Named numberFormat, and let's set it equal to NumberFormat..
-
1:15
And here we can see the different types of number format that we can create.
-
1:19
In addition to the standard getInstance() method,
-
1:22
we can also get a NumberInstance() for formatting a float, a CurrencyInstance(),
-
1:26
an IntegerInstance(), and a PercentInstance().
-
1:30
Let's go with getCurrencyInstance().
-
1:32
Next, let's create a new string variable to hold our formatted price,
-
1:38
String priceString, and let's set it equal to numberFormat.format() and
-
1:43
pass in our price.
-
1:47
Finally, let's call priceTextView.setText,
-
1:53
and pass in our price string.
-
1:56
Now if we run the app, And
-
2:00
pick a color, we've got 19,99, awesome.
-
2:05
But now it's in euro's Instead of dollars,
-
2:09
which makes it a completely different price.
-
2:12
For now, we're only pricing our paint in dollars, so we'll need to find a way to
-
2:16
get back to the dollar symbol while keeping the formatting we need.
-
2:20
Also, a quick note, depending on the device you have,
-
2:23
just changing the language might not be enough to update your locale.
-
2:28
Also, some devices just don't have that many languages available.
-
2:32
For example, my Note 4 only has five language choices.
-
2:36
So if you're unable to do something that I'm doing,
-
2:39
I'd suggest switching to an emulator.
-
2:41
Okay, so we need to find a way to keep the formatting, but
-
2:44
instead use the dollar symbol.
-
2:46
So how about, instead of using getCurrencyInstance(),
-
2:51
we use getNumberInstance().
-
2:53
And then when we're setting our formatted price,
-
2:57
we just tack on a dollar sign at the end.
-
3:00
But actually, if we're hard coding the currency sign to follow the price,
-
3:04
that means it won't be quite right for the English version,
-
3:07
where the currency symbol should be before the price.
-
3:10
Luckily, Android's got us covered here as well.
-
3:12
Let's start by undoing adding the dollar sign, and
-
3:16
changing this back to getCurrencyInstance().
-
3:18
Then, what we need to do, is set the currency symbol for
-
3:22
our number format to always be US dollars.
-
3:25
To do this, let's add a line below where we declare our number format, and
-
3:29
then call this set currency method on our number format object, so
-
3:33
numberFormat.setCurrency().
-
3:36
And then we need to pass in a currency.
-
3:38
So let's pass in Currency.getInstance(), which requires a country code.
-
3:44
So let's pass in "USD".
-
3:46
Now let's run the app again, And if we pick a color, perfect!
-
3:52
We've got 19,99 and then a dollar sign, and if we change our language back
-
3:58
to English, The United States version,
-
4:08
Then we've got a dollar sign, then 19.99.
-
4:12
Great job.
You need to sign up for Treehouse in order to download course files.
Sign up