加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何将双精度转换为int?

发布时间:2020-12-16 01:43:06 所属栏目:百科 来源:网络整理
导读:我有一个程序,使用一个公式计算一个单位的翻新(更换在损坏的电缆箱上的部件)除以总单位(通过翻新的电缆箱,但没有更换任何部件).我在网上查了一下,它的格式是: int valuetoconvert = Convert.ToInt32; 我这样做,但我仍然收到以下错误: Cannot implicitly co
我有一个程序,使用一个公式计算一个单位的翻新(更换在损坏的电缆箱上的部件)除以总单位(通过翻新的电缆箱,但没有更换任何部件).我在网上查了一下,它的格式是:

int valuetoconvert = Convert.ToInt32;

我这样做,但我仍然收到以下错误:

Cannot implicitly convert type ‘double to int. An explicit conversion exists(are you missing a cast?)

我究竟做错了什么?有人可以帮忙吗?谢谢.

这是我的一些代码:

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);
}

解决方法

你说你想要一个int cast,但你实际上需要强制转换为double.你可以这样做( Example Code):

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

否则,您需要将Refurb_Rate从double更改为int.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读