Comma decimal in the VS editor

1

It happens that the decimal separator, according to the Windows regional configuration is the comma, in Excel if I press the numerical keyboard the point appears a comma, the same happens with the calculator, but in the code editor of C #, if I put point appears point, and when declaring variables this is correct.

Double num1 = 176.55  //no hay problema

But if I have a TextBox and I put 45.67 and use

Double num2 = Convert.ToDouble(TextBox1.Text)

When executing, it generates an error with the decimal point and in the declaration of num1 there is no such error, but it does not generate an error with the decimal point. So every time I have to replace the comma with the point, and vice versa to insert the database (I'm used to it, but it bores me). The question, is there somewhere in the VS configuration, that my decimal separator is the comma. I want that when I write in design time Double x = 1,45 it does not appear red, because supposedly it's wrong. Thanks.

    
asked by Guidex 17.03.2018 в 06:49
source

3 answers

1

See this: link

It seems to me that VS does not have an option to change the separator (it gets it from the locale).

Personally, I prefer to define the culture at the project level.

System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone(); customCulture.NumberFormat.NumberDecimalSeparator = "."; System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;

    
answered by 17.03.2018 в 07:46
0

Store the data with the comma in a string always, when you go to operate it converts it to double ... And you put it back in a string. When you save it in the db, change the comma by period. And you only have to make one change, which is when you save it again. Because when you read it from the db and store it in a string, it remains with the comma. Greetings

    
answered by 17.03.2018 в 07:34
0

You should define a Culture object.

First, add the corresponding use.

using System.Globalization;

Then declare and initialize a CultureInfo object, then pass it as a parameter in the conversion.

CultureInfo cultura = new CultureInfo("es-AR");
//Remplazá "es-AR" por tu país.
decimal d = Convert.ToDecimal("TextBox1.Text", cultura);
//Caso decimal
double a = Convert.ToDouble("TextBox1.Text", cultura);
//Caso double

Greetings!

    
answered by 17.03.2018 в 21:25