decimal field minus comma c#

Asked

Viewed 247 times

0

Good afternoon,

I made an assignment as follows:

dadosRedistribuicao.QUANTIDADE = (Decimal.Parse(txtDisponivel.Text) * Decimal.Parse(percentual)) + Decimal.Parse(distribuicao);

data.QUANTITY is decimal type, so I am converting what comes from the text field. The percentage variable and distribution comes from a grid and step like this:

string distribuicao = gvMaterial.Rows[i].Cells[2].Text;
string percentual = gvMaterial.Rows[i].Cells[3].Text;

The value of txtDisponivel is 113, the value of the percentage is 0.77 and the value of the distribution is 1. the result was to be 88,01 however is coming 8702, someone can tell me the pq ?

thank you in advance.

  • what value is in the variable gvMaterial.Rows[i].Cells[3].Text ? 0.77 or 0,77 ?

  • 1

    0.77 however I am giving a replace with percentage = percentage. Replace(",", ".");

  • Probably the Parse is being done considering "." (point) as thousands separator. It should work if you inform 0,77 instead of 0.77. Or, rather, if you adjust the conversion from text to decimal number according to a pattern. The result you are getting is 113 * 77 + 1

1 answer

0


That Calculation:

    string distribuicao = "1";
    string percentual = "0,77";
    string disponivel = "113";

    decimal quantidade = decimal.Parse(disponivel) * decimal.Parse(percentual) + decimal.Parse(distribuicao);

Results: 8702

Dotnetfiddle

Right, that would be:

    string distribuicao = "1";
    string percentual = "0.77";
    string disponivel = "113";

    decimal quantidade = decimal.Parse(disponivel) * decimal.Parse(percentual) + decimal.Parse(distribuicao);

Upshot: 88.01

Dotnetfiddle

The only difference is the percentage separator.

  • 1

    Thank you so much, I can’t believe I made that bizarre mistake kkkkk was worth it worked out

Browser other questions tagged

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