如何将 double 转换为 int?

发布于 2024-11-01 02:17:22 字数 1231 浏览 4 评论 0原文

我有一个程序,它使用一个公式来计算一个单元的翻新(损坏的电缆盒上更换的部件)除以总单元(经过翻新的电缆盒,但没有更换任何部件)。我在网上查了一下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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千里故人稀 2024-11-08 02:17:22

你说你想要一个 int 类型转换,但实际上你需要转换为 double 类型。您可以这样做(示例代码):

Refurb_Rate = (double)totalRefurb / (double)totalUnits * 100d;

否则,您需要更改Refurb_Ratedoubleint

You say you want an int cast, but you actually need to cast to a double. You can do that like so (Example Code):

Refurb_Rate = (double)totalRefurb / (double)totalUnits * 100d;

Otherwise, you need to change Refurb_Rate from a double to an int.

仙女山的月亮 2024-11-08 02:17:22

你需要说你想要一个 int 强制转换:

double a;
int b = (int) a;

You need to say that you want an int cast:

double a;
int b = (int) a;
洛阳烟雨空心柳 2024-11-08 02:17:22

我不明白你的错误,因为 Refurb_Rate 是一个双精度值,其他所有内容都是一个整数。然而,我认为你真正想要的是这样的:

if (totalUnits != 0)
    Refurb_Rate = totalRefurb * 100.0 / totalUnits;

或者你可能想要这样的东西:

int Refurb_Rate = 0;
...
if (totalUnits != 0)
    Refurb_Rate = totalRefurb * 100 / totalUnits;

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:

if (totalUnits != 0)
    Refurb_Rate = totalRefurb * 100.0 / totalUnits;

Or possibly you want something like this:

int Refurb_Rate = 0;
...
if (totalUnits != 0)
    Refurb_Rate = totalRefurb * 100 / totalUnits;
她说她爱他 2024-11-08 02:17:22

试试这个:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文