【发布时间】:2011-04-11 17:23:26
【问题描述】:
我有一个程序,该程序使用公式计算单元的翻新(更换损坏的电缆盒上的零件)除以总单元(经过翻新但没有更换任何零件的电缆盒)。我在网上查了一下casting,格式是:
int valuetoconvert = Convert.ToInt32;
我正在这样做,但我仍然收到以下错误:
无法将类型 '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);
}
【问题讨论】:
-
Refurb_Rate = Convert.ToInt32(totalRefurb / totalUnits * 100d);
-
(没有 C# 经验,只有 C,所以只是评论)在 C 中,如果你在变量之前写 (int),它会将逗号后面的内容删除。如果要四舍五入,请使用 ceil() 和 floor() 或 round() 等函数
标签: c# types casting double int