简体   繁体   English

从浮点数获取整数的正确方法是什么?

[英]What is the proper way to get an int from a float?

I came across a problem and I think I have the solution but cannot find anyone to tell me if I am right or wrong? 我遇到一个问题,我想我有解决办法,但是找不到人告诉我我是对还是错? See sample in C# below: 请参见下面的C#示例:

public void Test(float number)
{
   object myObj = number;

  //Here is where I don't know what the correct way to do this is?
  // int newNumber = (int)myObj;
  // int newNumber = (int)(float)myObj;
 // int newNumber = (int)(double)myObj;
}

The result has to ensure no type exceptions occur. 结果必须确保不发生类型异常。 Any idea's guys? 有什么主意吗?

The second option is the right one. 第二种选择是正确的。

First, you unbox your value by casting it to the exact type that was originally boxed (ie float ). 首先, 通过将值转换为最初装箱的确切类型(即float )来取消装箱。
And then you convert float to int . 然后将float转换为int

Since the type of boxed value is float , you need to cast it to float first because it needs to be unboxed: 由于装箱值的类型是float ,因此您需要先将其强制转换为float因为它需要取消装箱:

int newNumber = (int)(float)myObj;

You can find more information from these questions if you wonder the reason of double cast: 如果您怀疑投的原因,可以从这些问题中找到更多信息:

Let's try each... 让我们尝试一下...

//int newNumber = (int)myObj;
//InvalidCastException: Specified cast is not valid.

int newNumber = (int)(float)myObj; // works! newNumber is 2

//int newNumber = (int)(double)myObj;
//InvalidCastException: Specified cast is not valid.

When you have such a boxed numeric value and wish to convert it with casting, you must first cast it to the exact type (in this case, float ) before converting it to a compatible type (in this case, int ). 当您拥有这样一个带框的数值并希望通过转换将其转换时,必须先将其转换为确切的类型(在这种情况下为float ),然后再将其转换为兼容的类型(在这种情况下为int )。

If you don't know ahead of time that it's a float , but you do know it can be converted to int , you should use Convert.ToInt32 , which will work whether myObj is a float , double , etc. 如果您事先不知道它是float ,但您确实知道它可以转换为int ,则应使用Convert.ToInt32 ,无论myObjfloatdouble还是其他类型,它都将起作用。

int newNumber = Convert.ToInt32(myObj);

According to your test you don't know type of boxed value then I'd avoid any double casting (because for first one you'll always need to know exact type ). 根据您的测试,您不知道装箱值的类型,那么我会避免进行任何重复转换 (因为对于第一个值,您始终需要知道确切的类型 )。

There is a class to work with conversion, let's try to use it: 有一个用于转换的类,让我们尝试使用它:

int number = Convert.ToInt32(myObj, null);

Actually that's pretty different from plain casting, it'll perform something like this: 实际上,这与普通铸造完全不同,它将执行以下操作:

int number = ((IConvertible)myObj).ToInt32(null);

Difference is how rounding is performed . 区别在于如何进行舍入 Cast will truncate your number (so 1.7 will become 1) while conversion will perform a rounding (so 1.7 will become 2). Cast将截断您的数字(因此1.7将变为1),而转换将进行舍入(因此1.7将变为2)。

If you're aware it's a floating point number (but you don't know if double or float ) then I'd suggest to first convert to double then cast: 如果您知道这是一个浮点数(但您不知道 doublefloat ),那么我建议您先转换为double然后进行转换:

int number = (int)Convert.ToDouble(myObj, null);

See also my answer about difference between casting and conversion . 另请参阅我有关转换和转换之间差异的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用颜色创建画笔的正确方法是什么? - What is the proper way to create a brush from a color? 从BlockingCollection中获取项目的正确方法是什么? - What is the proper way to take an item from a BlockingCollection? 从API更新应用程序的正确方法是什么? - What is the proper way to update an application from an API? 从C#关键字获取正确类型名称的最佳方法是什么? - What is the best way to get a proper type name from a C# keyword? 什么是通过Selenium Webdriver在C#中获取javascript对象的正确方法 - What is proper way to get javascript object by Selenium webdriver in c# 这是获取 WebProfile 的正确方法吗? - Is this a proper way to get a WebProfile? 从隐含的无符号十六进制字符串构造 BigInteger 的正确方法是什么? - What is the proper way to construct a BigInteger from an implied unsigned hexadecimal string? 从另一个类向Form输出数据的正确方法是什么 - What is the proper way to output data to Form from another class 在c#中取消订阅事件的正确方法是什么? - What's a proper way to unsibscribe from events in c#? 从语言文件绑定动态值的正确方法是什么? - What is the proper way to bind a dynamic value from a language file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM