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