I'm capturing from an HTML input that passes a string like C#
so:
string valor="12,5" o "15.5" //pueden ser valores con punto o coma
after having the value, I replace the dot with a comma:
string nuevoValor = valor.Replace(".", ","); //salida: 15,5
Currently my computer has a CurrentCulture
from es-ES
, when I try to convert the string
to double
this happens:
double valor1=Convert.ToDouble("15.5") //salida:155
double valor2=Convert.ToDouble("15,5") //salida:15.5 o aveces 15,5 no quiere depender de cuando le de la gana convertir a coma o no
If I apply this, if it converts correctly:
double valor3= Double.Parse("15,5",NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture); //salida: 15,5
I have to pass the application that I am doing to the computer that needs it, which has a CurrentCulture
de en-US
, which will not work since I had previously used InvariantCulture
it and it also gave me an error (according to what is InvariantCulture
used en-US
as default)
How can I convert a string that has a dot or a comma to a double with a comma regardless of what it CurrentCulture
has?
It's easy, just use the following function:
Cheers