Jus*_*tin 3 c# double int types casting
我有一个程序,使用一个公式计算一个单位的翻新(更换在损坏的电缆箱上的部件)除以总单位(通过翻新的电缆箱,但没有更换任何部件).我在网上查了一下,它的格式是:
int valuetoconvert = Convert.ToInt32;
Run Code Online (Sandbox Code Playgroud)
我这样做,但我仍然收到以下错误:
无法将类型'double隐式转换为int.存在显式转换(您是否错过了演员?)
我究竟做错了什么?有人可以帮忙吗?谢谢.
这是我的一些代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
你说你想要一个int cast,但你实际上需要强制转换为double.你可以这样做(示例代码):
Refurb_Rate = (double)totalRefurb / (double)totalUnits * 100d;
Run Code Online (Sandbox Code Playgroud)
否则,您需要Refurb_Rate从a 更改double为int.