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: