I have seen many users get confused about how different countries use different formats to display number format according to their country code. Most of the countries use “dot notation” as their separator, and many of the them use a “comma” as their separator. In this article, you will learn how to use a localized number format based on the country code and symbol to display using JavaScript’s tolocalestring() method, which returns a string.

To explain the concept of localization and number format based on their locale, I am using my previous article as a reference to display the price based on different countries and their currencies.

So, please check my previous article, How to Create Nested Grid using Angular 8. As a reference, I am fetching the price and displaying it with the currency symbol based on their country and currency code. Additionally, in the nested grid, I am displaying all prices with a currency symbol.

In this article, I am just trying to explain the concept of tolocalestring(), which I am going to apply to the prices we’re looking at. In my previous article, I just displayed price in “en” format, which is just a decimal number, but what if we want to see the price according to the respective country?

You may also like:
Angular 8: All You Need to Know.

Backend

To fetch all countries, you just have to add the following code in SQL Server:

After creating the table, it’s time to insert records in the country table. For that, I  have inserted the code, so just copy and paste in SQL Server.

In my previous article, I unfortunately forgot to give database records for product table so I am sharing that with you.

For Product Country Information, which is our nested grid, below is the insert query that I, unfortunately, forgot to share it in my last article.

Web API

Copy and paste the following code into your controller. This code will fetch all the countries that are in your database’s Countries table.

If you don’t have the complete code, I am sharing it with you below.

Complete Code for country controller:

Frontend

This following code will fetch all the countries and bind it to our drop-down.

This HTML code adds one drop-down which will bind all the countries which we are getting from database table country. Here, one method I have declared is onCountryChange, which fires when we change the drop-down selection.

Copy the following code and paste it in the product.component.html file inside the div that has the class, col-12 col-md-12.

Copy the following code and paste it or replace it with ngOnInIt(), which will get all the countries and bind them to our drop-down.

Copy the following code and paste it inside our service to fetch all the countries.

Next, copy this code and paste it inside the getProducts method, which will convert the price based on their country and display it in our browser.

 

 Then, paste the following code inside the showProductCountryInfo method.

    

This method is used to check if any country is selected or not; if no country is selected, then price will display normally without any currency symbol and currency format. On the other hand, if a country is selected, then the price will display according to their country code and currency symbol.

tolacalestring() is the JavaScript inbuilt method to convert a number to a string, which has some overloaded features, including country code and currency code (inside style curly braces), which we are fetching from the Countries table. 

This particular code is the method for changing the country. If $event is 0, no country is selected, and price will display normally. But, if the country code is present, then it will display the number according to their country currency code list.

To avoid any confusion, I am sharing the full code, so just replace that with the old code if you already have it from my previous article.

products.components.ts  

Also, I am sharing the complete source code so that it will be very easy to find the concerned output. Once you download the source code, then you just have to open it in VS Code and type npm install --save in the terminal. This will create a node_module file, and your project will run without any error. 

The following image is shown when you click on the plus icon to see the nested grid, which displays the price with all formats with their country name and currency symbol. 

For example, India uses dot notation for their decimal symbol with the rupee currency symbol.

 The following image is displayed when there is no country selected in the drop-down.

The following image is displayed when Sweden is selected in the drop-down.

The following image is displayed when Kuwait is selected in the drop-down.

The below image is the Countries table.

Further Reading



Source link

Write A Comment