Store decimal value

Asked

Viewed 818 times

2

I’m trying to take a typed point textbox value, and save it with decimal point, if you type 10.80, eh to store 10.8 in my variable.

I tried it this way:

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

But he’s saving everything together without the point.

  • Didn’t you mean "if you type 10.80 it is to store 10.8"? Because according to your code, you are explicitly removing the "." typed, so the example number is 1080.

2 answers

1


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

Must treat to have only one decimal point be it point (.) or comma (,)

Heed also regarding the language, if the language is different from pt-BR the result may be different.

  • 1

    It worked, vlw...

0

Make the value formatting:

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

Returns: 10.8

Reference: Custom Numeric Format Strings

Browser other questions tagged

You are not signed in. Login or sign up in order to post.