C# - Pass only numbers in a field formatted for currency

Asked

Viewed 226 times

-2

I have an application that presents me in a textbox the value of a course enrollment:

string mensalidade = string.Format("{0:C}", Convert.ToDouble(leitura["Mensalidade"]));
cursoSelecionado.Mensalidade = mensalidade;
tb_Mensalidade.Text = Convert.ToString(cursoSelecionado.Mensalidade);

As we can see, the field shows the value of this course formatted for the currency style. However, I need to pass only the value of this course, as decimal, to my database (SQL Server).

inserirMatricula.Parameters.Add("@Mensalidade", SqlDbType.Decimal).Value = ???;

How can I do that?

  • how is the value in leitura["Mensalidade"] ?

  • Of a course, as an example, is 1158.47. It is the value returned in the SQL table.

  • You could not pass this variable to your database? this formatting you presented 1158.47, sql server will perfectly accept.

  • No problem at all. The problem is that the field is formatted as a currency for a better user view (indicating a monetary field). Also, when the course value is an integer, for example, "875.00", SQL returns me without the decimals.

1 answer

1


I use this code. I assume the reading value["Tuition"] is as shown below.

    private void Parse()
    {
        decimal x = 0;
        string mensalidade = string.Format("{0:C}", Convert.ToDouble("23,3"));

        x = decimal.Parse(mensalidade, NumberStyles.AllowCurrencySymbol | NumberStyles.Number);

    }
  • And in case, I pass "tuition fee. Parse" in the parameters of Sqlcommand?

  • Yes, adjust the "Parse()" method and you can do something like inputMatricula.Parameters.Add(new Sqlparameter("@Tuition", Parse(tb_Mensalidade.Text));

Browser other questions tagged

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