Store decimal value

2

I'm trying to get a value from a typed textbox with a dot, and save it with dot in decimal, if you type 10.80, and then to store 10.8 in my variable.

I tried the following:

decimal valorTotal = Convert.ToDecimal(hfValor.Value.Replace(".", "").Replace(",", "."), CultureInfo.InvariantCulture);

However, he is saving everything together without the dot.

    
asked by anonymous 20.04.2015 / 20:12

2 answers

1
var textBoxValor = "10.8";
decimal variavel = Decimal.Parse(textBoxValor.Replace(".", ","));

You should treat to have only one decimal point whether it is dot (.) or comma (,)

Warning also regarding language, if the language is different from en-GB the result may be different.

    
20.04.2015 / 20:55
0

Format the value:

string valor = "10.80";
string valorFormatado = double.Parse(valor).ToString("n1");

Return: 10.8

Reference: Custom Numeric Format Strings

    
20.04.2015 / 20:26