0

I replaced the variables with values and the code work and sections of this work independently. Here is the section of code in question and it is the iCalcExample calculation I'm trying to correct.

if (Convert.ToInt32(args.ProposedValue) > 0) {
        // pending CommAmt_c
        string sCommAmt_c = (string) edvOrderDtl.dataView[edvOrderDtl.Row]["CommAmt_c"].ToString();
        int iCommAmt_c = Convert.ToInt32(edvOrderDtl.dataView[edvOrderDtl.Row]["CommAmt_c"]);
        // OrderDtl.DocExtPriceDtl
        string sExtPrice = (string) edvOrderDtl.dataView[edvOrderDtl.Row]["DocExtPriceDtl"].ToString();
        int iExtPrice = Convert.ToInt32(edvOrderDtl.dataView[edvOrderDtl.Row]["DocExtPriceDtl"]);
        // OrderDtl.DocDiscount
        string sDocDisc = (string) edvOrderDtl.dataView[edvOrderDtl.Row]["DocDiscount"].ToString();
        int iDocDisc = Convert.ToInt32(edvOrderDtl.dataView[edvOrderDtl.Row]["DocDiscount"]);
        // Calculated value
        int iCalcExample = Convert.ToInt32((iCommAmt_c) / (iExtPrice - iDocDisc)) * 100;
        // string sCalcExample = iCalcExample.ToString();
        // string sCalcExample = String.Format("{0:F?}", iCalcExample, 2);
        int numberOfDecimalPlaces = 2;
        string formatString = String.Concat("{0:F", numberOfDecimalPlaces, "}");
        string sCalcExample = string.Format(formatString, iCalcExample);

        // end variables
        MessageBox.Show("ExtPrice: " + sExtPrice);
        MessageBox.Show("DocDisc: " + sDocDisc);
        MessageBox.Show("Calc Example: " + sCalcExample);

    }
Ankit Katiyar
  • 2,631
  • 2
  • 20
  • 30
  • 3
    What are the values used in the calculation? I bet that iCommAmt_c/(iExtPrice - iDocDisc) is less than 1.00 and gets truncated to 0. Multiply by 100 first rather than after. – Jay Buckman May 12 '20 at 13:26
  • (15000 / (150000 - 200)) * 100. It does come out to less then 1 (0.10) before multiplying by 100. – Bruce H May 12 '20 at 14:13
  • Or based on the notation they are integers and integer math is used – pinkfloydx33 May 12 '20 at 14:18
  • Hi Jay, thanks for your suggestion. I tried this: 100*(iCommAmt_c/(iExtPrice - iDocDisc)) and it still returned a zero value. I'm thinking instead of converting to int I should be converting to decimal. – Bruce H May 12 '20 at 14:21
  • Hi pinkfloydx33, thanks for your suggestion. It looks like that addressed the same problem. I'll give it a try. – Bruce H May 12 '20 at 14:24
  • Thank you both! I changed the code to int iCalcExample = (Convert.ToInt32(iCommAmt_c)*100) / (iExtPrice - iDocDisc); and it is now working properly. – Bruce H May 12 '20 at 14:32

0 Answers0