ASP.NET–How to format currency based on user’s browser setting
Posted by Jean Paul on November 11, 2010
Hello All. In this example I am trying to show how to format currency based on user’s browser setting.
Our aim is to show them the formatted currency like:
Format | Language |
$1,000.00 | United States |
1,000.00د.إ. | Arabic |
R$ 1.000,00 | Portugese |
1.000,00 TL | Turkish |
Step 1: Create the ASP.NET Application
Create a new web application and on the default page place a button and label.
On the button click you can add the following code.
protected void Button1_Click(object sender, EventArgs e)
{
CultureInfo cultureInfo = CultureInfo.CreateSpecificCulture(this.Request.UserLanguages[0]);
double value = 1000;
Label1.Text = value.ToString("C", cultureInfo.NumberFormat);
}
The code collects the user language from Request object and uses the ToString() method to format the value.
That finishes the coding part.. Now you have to see the things working. For this you have to run the application and test it by changing the browser language. I will guide you through it.
Step 2: Execute the application
You can execute the application and see the page opened in your browser.
Try clicking the button and you can see the number formatted in current language.
Step3: Change Browser Language
In this step we have to change the browser language.
If you are using Internet Explorer choose Internet Options screen.
Click the Languages button on the bottom and you can see the following screen.
Click the Add button to add a new language. Select Arabic, move it up using the “Move up” button and click Ok.
Now you are ready to test the code in Arabic.
Click the “Change Format” button again and you can see the number format in Arabic.
Note: After changing the languages to back in browser you might need to restart it.
Leave a comment