-2

i have two textboxes 1 called 'mtb_NETPAIE02' and 2 called 'mtb_TAXE02' the format in the two textboxes is currency/money (double) i want to fix the error on this Code :

if (double.Parse(mtb_NETPAIE02.Text) >= 100001 )
{
    mtb_TAXE02.Text = (double.Parse(mtb_NETPAIE02.Text) / 5000 * double.Parse("12") 
                       + double.Parse("18").ToString("N2")).ToString();               
}

12 (12,00) and 18 (18,00) are money but 5000 is int .

how to make this right ?

Mikhail Tulubaev
  • 4,141
  • 19
  • 31
Nouri Yacine
  • 63
  • 13

1 Answers1

1

First of all, you should not be using double if you're working with money; you should be using decimal.

Secondly, you should use the literals for decimal, i.e. with the M suffix.

Finally, you are attempting to add the result of decimal.Parse("18") to the other numeric values, but you are converting it to a string first via ToString("N2"). You should move the latter outside your parens to convert the whole result from the calculation into a string:

if (decimal.Parse(mtb_NETPAIE02.Text) >= 100001M )
{
    // notice the `M` in 5000M
    mtb_TAXE02.Text = (decimal.Parse(mtb_NETPAIE02.Text) / 
        5000M * decimal.Parse("12") + decimal.Parse("18")).ToString("N2");
}
Community
  • 1
  • 1
rory.ap
  • 34,009
  • 10
  • 83
  • 174