How do I cast a double to an int?
-
Hi, I have a program for work that uses a formula to calculate the refurb on a unit(parts replaced on cableboxes that were damaged) divided by total units(cableboxes that went through refurb, but did not have any parts replaced). I looked up casting online, and the format for it is intvaluetoconvert = Convert.toint32; I'm doing that , but still get the error Cannot implicitly convert type 'double to int. An explicit conversion exists(are you missing a cast?) What am I doing wrong? Can someone please help? Thank you. Justin Here's some of my code to look at? private int GetRefurbRate() { string sql = ""; double Refurb_Rate; int totalRefurb = 0; int totalUnits = 0; string error_msg = ""; sql = "SELECT COUNT(rp.repair_ord) " + "FROM " + schema + ".repair_part rp " + "WHERE rp.repair_ord = '" + repair_ord + "' "; while (true) { if (!myDb.RunSql(sql, true)) { error_msg = "DBError for getting Refurb Rate"; break; } if (myDb.dbRdr.HasRows) { if (myDb.dbRdr.Read()) { try //Try and Catch are here b/c I originally had everything ints, and and they just caught the 0 exception. { Refurb_Rate = Convert.ToInt32( totalRefurb / totalUnits * 100); //This is where I try to perform the cast. } catch (Exception e) { Console.WriteLine(e); } } //int Refurb_Rate = Convert.ToInt32(Refurb_Rate); } break; } myDb.dbRdr.Close(); if (error_msg != String.Empty) { MessageBox.Show(error_msg, "Get Refurb Rate", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } return Refurb_Rate; }
Give this a try this should fix your problem;
return (int)Refurb_Rate;