C# typecast Int to Double

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SpecialKay
    New Member
    • Mar 2008
    • 109

    C# typecast Int to Double

    This might be an eazy problem to fix,
    assume all works except the int to double
    [code=cpp]
    double total = 0;
    double Qty[10];

    int price[10];

    for(int i = 0; i < 9; i++)
    {
    total += Qty[i] * (double)price[i];
    }

    [/code]

    i need to get total in the forme of a double.
    any ideas?
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    EDIT:
    Actually, I think your example might be a little off. Wouldn't you want price to be a double and quantity to be the int? Your array declarations seemed to be off as well, since those aren't valid declarations.

    I just did this, without typecasting at all and total stayed a double:
    Code:
    double total = 0;
                double[] Qty = new double[10];
                Qty[0] = 1.1;
                Qty[1] = 4.5;
                Qty[2] = 9.7;
                Qty[3] = 2.6;
                Qty[4] = 0.7;
    
                int[] price = new int[10];
                price[0] = 1;
                price[1] = 4;
                price[2] = 3;
                price[3] = 7;
    
                for (int i = 0; i < 9; i++)
                {
                    total += Qty[i] * price[i];
                }
    Total was a double just fine. So I am thinking in your ACTUAL code there is something else wrong?

    Comment

    • SpecialKay
      New Member
      • Mar 2008
      • 109

      #3
      here is the Accualy code

      [code=cpp]
      infos.TotalCost += (double)(infos. Order.OrderLine ItemCollection[i - 1].Quantity * double.Parse(in fos.Order.Order LineItemCollect ion[i - 1].Price));

      [/code]

      broken down

      [code=cpp]
      infos.TotalCost = Double
      infos.Order.Ord erLineItemColle ction[i - 1].Quantity -> Returns int
      infos.Order.Ord erLineItemColle ction[i - 1].Price -> returns String (double.Parse works for this)

      [/code]

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Your actual code looks like it should work fine?

        Comment

        • SpecialKay
          New Member
          • Mar 2008
          • 109

          #5
          Ya i agree, there must be a problem somewhere else.
          im going to snoop around and see what i come up with.
          In other news:

          if you multiply an int (2) by a double (1.3) into a double like

          [code=cpp]
          double ans = 0;
          int a = 2;
          double b = 1.3;

          ans = a*b;

          does ans become an int?
          what would the value of ans be?
          [/code]

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            It should be a double.
            You can run all these testcases yourself to see the answer quickly though.

            Comment

            • SpecialKay
              New Member
              • Mar 2008
              • 109

              #7
              you are write :)
              thanks tho, i did after i posted the question, so i really didnt need to post it. but o well.

              Comment

              Working...