如何将 double 转换为 int?
我有一个程序,它使用一个公式来计算一个单元的翻新(损坏的电缆盒上更换的部件)除以总单元(经过翻新的电缆盒,但没有更换任何部件)。我在网上查了一下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);
}
I have a program 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:
int valuetoconvert = Convert.ToInt32;
I'm doing that, but I still get the following 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.
Here's some of my code:
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你说你想要一个 int 类型转换,但实际上你需要转换为 double 类型。您可以这样做(示例代码):
否则,您需要更改
Refurb_Rate
从double
到int
。You say you want an int cast, but you actually need to cast to a double. You can do that like so (Example Code):
Otherwise, you need to change
Refurb_Rate
from adouble
to anint
.你需要说你想要一个 int 强制转换:
You need to say that you want an int cast:
我不明白你的错误,因为
Refurb_Rate
是一个双精度值,其他所有内容都是一个整数。然而,我认为你真正想要的是这样的:或者你可能想要这样的东西:
I don't understand your error, because
Refurb_Rate
is a double and everything else is an int. However, I think what you really want is this:Or possibly you want something like this:
试试这个:
Refurb_Rate = (int)((double)totalRefurb /totalUnits * 100);
确保将 int 设置为 double,否则 1 / 2 将等于 0,而不是 0.5
Try this:
Refurb_Rate = (int)((double)totalRefurb / totalUnits * 100);
Make sure you case the int to double otherwise 1 / 2 will equal zero instead of .5