Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect decimal separator

I have to detect decimal separator in current windows setting. Im using visual studio 2010, windows form. In particular, if DecimalSeparator is comma, if user input dot in textbox1, I need show zero in textbox2.

I tryed with this code, but not works:

private void tbxDaConvertire_KeyPress(object sender, KeyPressEventArgs e)     {         string uiSep = CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator;         if (uiSep.Equals(","))         {             while (e.KeyChar == (char)46)             {                 tbxConvertito.Text = "0";             }         }      } 

I have tryed also this code, but not work:

private void tbxDaConvertire_KeyPress(object sender, KeyPressEventArgs e)     {         string uiSep = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;         if (uiSep.Equals(","))         {             if (e.KeyChar == (char)46)             {                 tbxConvertito.Text = "0";             }         }      } 
like image 637
Vincenzo Lo Palo Avatar asked Jan 25 '13 00:01

Vincenzo Lo Palo


People also ask

What is the correct decimal separator?

This symbol can be a period ("."), as is common in United States and other English-speaking countries, or a comma (","), as in continental Europe. Decimal point and decimal comma are also common names for the decimal separator.

How do you change the decimal separator?

Click File > Options. On the Advanced tab, under Editing options, clear the Use system separators check box. Type new separators in the Decimal separator and Thousands separator boxes.

Why is there a comma instead of decimal?

as a separator between the dollars and cents. Some countries use a comma (,) instead of a decimal to indicate that separation. In addition, while the U.S. and a number of other countries use a comma to separate thousands, some countries use a decimal point for this purpose.


2 Answers

Solution:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) {     char a = Convert.ToChar(Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator);     if (e.KeyChar == a)     {         e.Handled = true;         textBox1.Text = "0";     } } 

That way, when you hit . or , you will have a 0 in your TextBox.

EDIT:

If you want to insert a 0 everytime you hit the decimal separator, this is the code:

char a = Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator); if (e.KeyChar == a) {     e.KeyChar = '0'; } 
like image 147
Andres Avatar answered Oct 03 '22 17:10

Andres


Actually you should be using

CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator 

instead of

CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator 

Using the second one gives you the OS default settings, which might be different then user Regional Locales for particular user account logged to this PC.

Credits to berhir and Grimm for pointing out the [docs]

like image 41
Bart N Avatar answered Oct 03 '22 18:10

Bart N