System.Convert.ToInt VS(int)的

编程入门 行业动态 更新时间:2024-10-26 20:35:34
本文介绍了System.Convert.ToInt VS(int)的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我注意到在另一篇文章,有人做了这样的:

I noticed in another post, someone had done something like:

double d = 3.1415; int i = Convert.ToInt32(Math.Floor(d));

为什么他们使用convert函数,而不是:

Why did they use the convert function, rather than:

double d = 3.1415; int i = (int)d;

有一个隐含的地板和转换。

which has an implicit floor and convert.

此外,更令人担心的,我在一些生产代码我读注意到:

Also, more concerning, I noticed in some production code I was reading:

double d = 3.1415; float f = Convert.ToSingle(d);

是一样的:

float f = (float)d;

是所有那些否则隐式转换只是出于完整性Convert类,还是他们服务于一个目的?我可以理解,需要的ToString(),而不是休息。

Are all those otherwise implicit conversions just in the Convert class for completeness, or do they serve a purpose? I can understand a need for .ToString(), but not the rest.

推荐答案

铸造为int是隐含截断,不隐地板:

Casting to int is implicit truncation, not implicit flooring:

double d = -3.14; int i = (int)d; // i == -3

我选择Math.Floor或Math.Round到使我的意图更加明确。

I choose Math.Floor or Math.Round to make my intentions more explicit.

更多推荐

System.Convert.ToInt VS(int)的

本文发布于:2023-11-07 06:51:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565813.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Convert   System   int   ToInt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!