c# – 如何将双精度转换为int?
发布时间:2020-12-16 01:43:06 所属栏目:百科 来源:网络整理
导读:我有一个程序,使用一个公式计算一个单位的翻新(更换在损坏的电缆箱上的部件)除以总单位(通过翻新的电缆箱,但没有更换任何部件).我在网上查了一下,它的格式是: int valuetoconvert = Convert.ToInt32; 我这样做,但我仍然收到以下错误: Cannot implicitly co
我有一个程序,使用一个公式计算一个单位的翻新(更换在损坏的电缆箱上的部件)除以总单位(通过翻新的电缆箱,但没有更换任何部件).我在网上查了一下,它的格式是:
int valuetoconvert = Convert.ToInt32; 我这样做,但我仍然收到以下错误:
我究竟做错了什么?有人可以帮忙吗?谢谢. 这是我的一些代码: 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. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ruby-on-rails – 使用URL哈希(window.location.hash)进行R
- c# – 如何访问绑定到DevExpress XtraTreeList中的节点的对
- 详解vue.js+UEditor集成 [前后端分离项目]
- objective-c – NSTimer问题
- 在Ruby中提取由Selenium WebDriver发布的浏览器的pid
- c – 链接对象和静态库
- Acwing-204-表达整数的奇怪方式(扩展中国剩余定理)
- xml – 如何使用XSLT/XPath生成逗号分隔的列表?
- c – 隐式转换将signedness’int’更改为’unsigned int’
- ios – 静态单元格在iPhone上有背景颜色,但在iPad上没有 –