decimal point is lost in the getter and setter

0

I have a problem with some getter and setter, these are the properties

public double PrecioUnitario
{
    get { return _PrecioUnitario; }
    set { _PrecioUnitario = value; }
}

private double _SubTotal;

public double SubTotal
{
    get { return _SubTotal; }
    set { _SubTotal = value; }
}

To these properties I assign values with a decimal point retrieved from textboxes

    EntProyectoGto.PrecioUnitario = Convert.ToDouble(txtPrecioUnitario.Text);
    EntProyectoGto.SubTotal = Convert.ToDouble(txtSubtotal.Text);

but I realize with a break point in my project that when the value is passed to the property it no longer receives the decimal point, in the image the first arrow shows the value that the property receives (1140) and in the arrow below I show the value received from the textbox (11.40)

    
asked by Ivxn 04.01.2018 в 05:57
source

2 answers

2

Good Ivxn,

The problem you have here is that, depending on the region where you are running the program, you will detect the decimal separator as a ' . ' or a ' , ', in your case you it will detect like a comma and when doing the Convert.ToDouble it will remove the point since it does not recognize it.

You can solve this by putting the following:

EntProyectoGto.SubTotal = Convert.ToDouble(txtSubtotal.Text.Replace('.',','));
    
answered by 04.01.2018 в 08:15
0

The problem is due to the regional configuration configured by the server where the web application is running.

For example, in my case the decimal symbol is the period (.) and the separation of thousands is the comma (,).

You must understand that converting a cadena to a doble makes use of the regional settings.

For example:

Console.WriteLine("Separador miles: " + CultureInfo.CurrentCulture.NumberFormat.CurrencyGroupSeparator); // Separador miles: ,
Console.WriteLine("Separador decimal: " + CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator); // Separador decimal: .

Console.WriteLine(double.Parse("11.40")); // 11.4
Console.WriteLine(double.Parse("114,0")); // 1140
Console.WriteLine(double.Parse("11,40")); // 1140       
Console.WriteLine(double.Parse("1,140")); // 1140       
Console.WriteLine(double.Parse("114,0.40")); // 1140.4      
Console.WriteLine(double.Parse("11,40.40")); // 1140.4      
Console.WriteLine(double.Parse("1,140.40")); // 1140.4

Console.WriteLine(1140.ToString("C")); // $1,140.00
Console.WriteLine(11.40.ToString("C")); // $11.40

Console.WriteLine();
NumberFormatInfo nfi = new CultureInfo("en-US", false ).NumberFormat;

//      
Console.WriteLine(123456789.ToString("C", nfi )); // $123,456,789.00        

// Configurando para que el formato considere:
nfi.CurrencyDecimalSeparator = ",";
nfi.CurrencyGroupSeparator = ".";

Console.WriteLine(123456789.ToString("C", nfi )); // $123.456.789,00    

You'll get:

Separador miles: ,
Separador decimal: .
11.4
1140
1140
1140
1140.4
1140.4
1140.4
$1,140.00
$11.40

$123,456,789.00
$123.456.789,00

PD. the Convert.ToDouble method encapsulates the double.Parse method.

If you had problems not being able to alter the regional configuration, in that case you should use a different culture, where you use the comma (,) as a decimal symbol and the period (.) as a symbol of thousands.

For example:

CultureInfo elGR = CultureInfo.CreateSpecificCulture("el-GR");
Console.WriteLine(double.Parse("11,40", elGR)); // 11.40
Console.WriteLine(double.Parse("11.40", elGR)); // 1140

You'll get:

11.4
1140

DEMO

Reference:

answered by 04.01.2018 в 08:16