Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

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
 
Share this answer
 
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
 
Share this answer
 
v2
Comments
Mohanraj Selvarathinam 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 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 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 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900