Click here to Skip to main content
12,451,227 members (54,600 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
I`ve made an app and send it to my friends and professor. Professor is using matlab, mathematica,... so the decimal character in windows was changed from comma (,) to dot (.), and gets wrong results.
How to make the app that uses character for decimal which is set in control panel --> regional setting in windows? So if user has dot for decimal then insert dot if comma then insert comma, and in both cases characters means decimal.
I tried with:
a = double.Parse(textBox1.Text, System.Globalization.NumberStyles.Number, System.Globalization.NumberFormatInfo.CurrentInfo);
but it does not work.

How to fix this?
Posted 4-Jan-13 2:52am
Comments
CHill60 4-Jan-13 9:10am
   
This works for me - but only after a short delay. In other words if I change my regional settings then run this I get "12" reported in Output window, but if I (immediately) then run it again I get "1,2" as expected.
I have to say that when using decimals "internally" within a program you shouldn't have to worry about whether '.' or ',' is used - as long as the presentation/data capture layer can handle it properly
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Did you try it without numberformat?
a = double.Parse(textBox1.Text, System.Globalization.NumberFormatInfo.CurrentInfo);

Also check the example here:
http://msdn.microsoft.com/en-us/library/t9ebt447.aspx[^]

Good luck!
  Permalink  
Comments
marcel zol 4-Jan-13 10:15am
   
Ok, i`m working on your sample, but what to do with selected items in combobox? I`ve added items in Collection... with comma. Items will always be with comma. Anything for fixing this?
Is it the only option to add them throw code?
E.F. Nijboer 4-Jan-13 10:48am
   
I do not fully understand what you mean exactly. But if you if you are sure that numbers will always be in the same culture, you can simply use that. Guessing by your profile you want something like :
a = Double.Parse(textBox1.Text, new CultureInfo("us-US"));
marcel zol 5-Jan-13 8:07am
   
I`ve use your sample:
a=double.Parse(textBox1.Text,System.Globalization.NumberFormatInfo.CurrentInfo)
and it works great, but i have two comboboxes that have two numbers 1,5 and 1,15 in it. I added them in Properties --> Collection... they will always be with comma. Question is how to fix this and if it is possible to fix this not by adding 1,5 and 1,15 programmatically.
If I use your sample, and if i change region settings in windows so decimal means dot, my app shows everything with dot, except those two in combobox are sill with comma (because i added them in Collection...).
E.F. Nijboer 5-Jan-13 11:10am
   
You could set the culture of the application always to "us-US" for the entire application. Just as Bernhard Hiller suggest, set the culture of the application in the initialization.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Set the culture of your application to "en-US" early, e.g. in public static int Main(string[] args):
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
  Permalink  
Comments
marcel zol 5-Jan-13 12:34pm
   
I`ve put it like this:
public Form1()
{
InitializeComponent();
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
}
is this right?
So it means that user will have to put in decimal character that is for the en-US, despite having the configuration in regional settings in Windows changed for decimal character, let say "\" or anything else.

So can i now remove the System.Globalization.NumberFormatInfo.CurrentInfo in:
textBox6.Text = fcd.ToString("0.000",System.Globalization.NumberFormatInfo.CurrentInfo);
?
Bernhard Hiller 7-Jan-13 2:21am
   
Exactly. Why didn't you just try it?
marcel zol 7-Jan-13 14:36pm
   
I already have, thanks. Works like a charm.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 4 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100