Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0ASP.NET
I have problem in localizing currency value in asp.net
 
The idea is that I want to display the currency value for the Danish (da-DK) culture and the same value will be store in DB as english format . I am using NumberFormatInfo to change to currency for the selected culture.
 
Example:
 
if I select Danish (da-DK) ,the currency value is 100.000,56
 
I want to store the value in Database is that 100000.56
 

The code is below.
 
TextBox1.Text = 100.000,56;
 
double dblDBvalue ;
 
dblDBvalue=Convert.ToDouble(TextBox1.Text.ToString() == "" ? "0" : TextBox1.Text.ToString(),NumberFormatInfo.InvariantInfo );
 
This code is giving throw error, when I want to convert from Danish to englsh.
 
thanks in advance.
Posted 26-Sep-12 3:08am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Just one simple hint which should resolve everything: in dabatased and data management, and, more generally, almost anywhere except the UI, don't work with string representation of data, work with data itself. Use appropriate database data types, such as numeric. Not only you save on space and processing of data, you save yourself from trouble. Avoid parsing strings and invalid data; you parse or interpret data only when if comes from the user input, which is culture sensitive, error prone but human-readable,
 
Cultures is just one additional benefit of it. Data is culture-neutral, its string representation is not. Your 100.000,56 in binary form is the same in all cultures. If this is floating-point, it is governed by standard IEEE 754 (see), and so on.
 
—SA
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Just one simple hint which should resolve everything: in dabatases and data management, and, more generally, almost anywhere except the UI, don't work with string representation of data, work with data itself. Use appropriate database data types, such as numeric. Not only you save on space and processing of data, you save yourself from trouble. Avoid parsing strings and invalid data; you parse or interpret data only when if comes from the user input, which is culture sensitive, error prone but human-readable,
 
Cultures is just one additional benefit of it. Data is culture-neutral, its string representation is not. Your 100.000,56 in binary form is the same in all cultures. If this is floating-point, it is governed by standard IEEE 754 (see), and so on.
 
[EDIT]
 
In response to comments to this answer:
 
Please see my comment where I advice to rely on the culture specified as the culture of your thread; there are two separate properties: System.Threading.Thread.CurrentCulture and System.Threading.ThreadCurrentUICulture:
http://msdn.microsoft.com/en-us/library/system.threading.thread.currentculture.aspx[^],
http://msdn.microsoft.com/en-us/library/system.threading.thread.currentuiculture.aspx[^].
 
You can assign the culture at any time during run-time. The satellite assemblies will be loaded according the fallback mechanism I described in comments below. Of course, it won't change the look of the application immediately, as the data from resources will come up only when you use them, so you will need to invalidate anything which is involved and culture-dependent.
 
In your explicit calculations, the CurrentCulture of the thread affects the behavior of the methods which are not culture-neutral. Besides, the API allows you to pass culture as a parameter. Let's consider, for example, double.ToString:
http://msdn.microsoft.com/en-us/library/shxtf045.aspx[^],
http://msdn.microsoft.com/en-us/library/d8ztz0sa.aspx[^].
 
Try to use them passing the instance of the System.Globalization.CultureInfo where the parameter of the interface type System.IFormatProvider is expected. You can do it because this class implements this interface. If you do that, the string will be formatted as required by the specified culture. Please see:
http://msdn.microsoft.com/en-us/library/system.iformatprovider.aspx[^],
http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx[^].
 
—SA
  Permalink  
v2
Comments
Mohanraj Selvarathinam at 27-Sep-12 6:29am
   
Thanks for your reply. Yes. In the database, I have defined datatype as numeric(18,4). for example : This is the value 100000.5600 will be stored in the DB. While displaying if I select Danish (da-DK) ,the currency value will be displayed as 100.000,56 if I select english (en-US),the currency value will be displayed as 100,000.56 if I select english (en-IN),the currency value will be displayed as 1,00,000.56 After modification I want to store the value in Database in this format : 100000.56 Now I am facing problem in conversion from Danish (1,00,000.56) to DB format (100000.56) Can you clarify it.
Sergey Alexandrovich Kryukov at 27-Sep-12 12:20pm
   
If it is numeric, it is not effected by cultures. You take care of cultures when you call ToString(...). Or your current UI thread culture, being set up to require culture, takes care about that. Do you know how to do that, and how to globalize and localize, generally? About satellite assemblies, etc.? --SA
Mohanraj Selvarathinam at 28-Sep-12 3:18am
   
Thanks for your reply. I am new to localization of web application. Yes. I have created satellite assemblies(.resx) file for each culture for displaying label,text, images, messages information. I need use currency separator, while displaying in UI and I need to remove currency separator, while storing to DB. To do I am using numberformatInfo, which is .net class library. Only I am facing problem in conversion from Danish Currency format (1,00,000.56) to DB format (100000.56). Other culture is conversion working fine.
Sergey Alexandrovich Kryukov at 28-Sep-12 13:47pm
   
The approach is that you should review all APIs like "Parse..." and "ToString" and find out what is culture-dependent. Typically, some of those functions are either dependent from the thread UI culture, some have System.Runtime.InteropServices.IFormatProvider parameter, and this interface is implemented by CultureInfo which you can use. Oops! I tried to give your references... I see it for the first time: MSDN is not connecting, showing failure message... MSDN is under attack :-) No matter, you can find it by yourself. Now, you can use System.Threading.Thread.CurrentCulture and System.Threading.Thread.CurrentUICulture to switch the culture. It will cause loading appropriate satellite assemblies automatically. Moreover, if the resources for exact culture are missing, the fallback mechanism will chose the "closest culture" satellites, and, if failed, will fallback all the way to the original culture represented in the main (non-satellite) resources. Now, using the idea and this information as keywords, you can find it all in MSDN by yourself. How about that? Of course, if you have further questions, they are very welcome, but if this is enough to get started, please accept the answer formally (green button). Wait a minute: it looks like MSDN is back... --SA
Sergey Alexandrovich Kryukov at 28-Sep-12 13:59pm
   
Done... I hope that now you have the complete picture. In addition to your original question, it also gives you the idea how your satellite comes into play. --SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 412
1 sanket saxena 355
2 Abhinav S 258
3 OriginalGriff 235
4 CPallini 205
0 Sergey Alexandrovich Kryukov 8,704
1 OriginalGriff 5,000
2 Peter Leow 3,839
3 Maciej Los 3,535
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 28 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid