Formatting Numbers for International Use
00:00
In an earlier lesson, you used a variable value =
12345.6789
. Doesn’t really matter what this number is, but it’s a good one to use.
00:09
You may recall, for example, you could display the value in an f-string and then use as a format specifier the comma for a thousand separator .2f
to show you want two digits after the decimal point.
00:23
And this would give you your 12,345.68
. However, this format is not the standard format in every country in the world. In some countries, there are different conventions on what to use as a decimal point and what to use as a thousand separator.
00:40
So if you want to display your number based on your own country or the country where your users are, f-strings can help you with this as well. First, you need to import the locale
module, which is part of the standard library and it helps you deal with international localizations.
00:55 And I’m going to paste in a set of sample locales. This is a list of tuples and each tuple consists of a pair, the name of the country and the code that’s used to set the localization.
01:08
And you can now loop through this list of tuples, let’s say for name, loc in sample_locales:
which is the list you just created.
01:19
Now the first thing you want to set is you want to change the locale in your environment. And the locale
module allows you to set locale
.
01:28
And here you need to decide what category you want to use. For example, do you want to only change the number formats, the currencies? In this case, you can choose everything using locale.LC_ALL
, the LC_ALL
constant.
01:41
And finally, you can set the locale call itself to the loc
, which is the variable you defined in the for
loop statement. Now this will work, but this will also return the function setlocale()
returns, the locale you’ve just set.
01:57 If you want to avoid displaying this in the terminal, you can assign this to a dummy variable such as the underscore.
02:05
And now that you’ve set the correct locale, each time the for
loop iterates, you can print an f-string to display what’s happening. So for example, you can put the country there so the name of the country uses and you want the value.
02:19
Now how can you format your value based on the localization? And there’s a format specifier for this, and that’s the letter n
. And this gives you the format that fits the localization you have currently set.
02:33 And as you can see, these countries use different formats. So the USA, UK, and Japan all have the comma for the thousand separator and the dot as the decimal point.
02:43 However, Poland uses a comma instead of a decimal point and this is common in many countries. You can see the rest of the countries also have commas instead of decimal points.
02:53 And whereas Poland and Czech Republic put a space as a thousand separator in other countries, Brazil is one of these examples it seems the dot is used. So for example, in the convention used in Brazil and many other Latin American countries, the dot and the comma are reversed.
03:10 The comma is used as a decimal point and the dot is used as a thousand separator.
03:15
And let’s also look at how to deal with currencies. You can run the for
loop again. You still need to set the locale in each iteration of the for
loop.
03:24 But in this case, let’s print something else.
03:28
And let’s still put in the name of the country uses. But now what we want to do is show the number as a price in the local currency. So you can use the currency()
function in the locale
module that you’ve imported.
03:43
And this requires the parameter val
, which you can assign to value
, the variable you created earlier.
03:51 And as you can see, although the number’s the same, it is displayed using the currency symbol for the appropriate countries and for those countries where the currency symbol goes after the price, then that is also taken into account.
04:05
You can even go a bit further if we run the for
loop again in a similar fashion but you can also add the grouping
named argument and set it to True
.
04:18 And there you can see that where appropriate, the thousand separator has gone in and for those countries which use different thousand separators, the appropriate character is used.
04:30
Therefore, f-strings help you deal with displaying numbers in any international format you want to aim for. You can use the n
format specifier to represent this in your f-strings and you need some help from the locale
module, which is a module in the standard library that provides you with the tools you need for international localization.
Become a Member to join the conversation.