[英]Convert.ToInt32 behavior string vs float/double literal
Convert.ToInt32
behaves different when passed string
vs float/double
literal 传递string
vs float/double
literal时, Convert.ToInt32
行为会有所不同
var result = Convert.ToInt32(12.4);//returns 12
result = Convert.ToInt32(12.44);//returns 12
result = Convert.ToInt32(12.4444444);//returns 12
result = Convert.ToInt32("12.4"); // Input string was not in a correct format.
I understand different overloads of Convert.ToInt32
are being called for string and float/double 我理解为字符串和float / double调用Convert.ToInt32
不同重载
The question is why this inconsistent behavior shouldn't single
overload for Convert.ToInt32
throw an exception for loss of precision ? 问题是为什么这种不一致的行为不应该single
重载Convert.ToInt32
抛出一个精度损失的异常?
The way I see it, when you start with an float
/ double
and you convert to int
you expect a loss of precision. 我看待它的方式,当你以float
/ double
并转换为int
你会发现精度损失。 When you have a string that you convert to int you don't expect the parsing to do any losing of data, you just want it to parse and fail if the string is not valid. 当你有一个转换为int的字符串时,你不希望解析会丢失任何数据,你只是希望它解析并在字符串无效时失败。
The question is why this inconsistent behavior shouldn't single overload for Convert.ToInt32 throw an exception for loss of precision ? 问题是为什么这种不一致的行为不应该单独重载Convert.ToInt32会抛出一个精度损失的异常?
You can think of the utility methods you're currently using to convert from double
to int
as "casting" ie (int)12.4
, (int)12.44
etc. which in essence means you for sure know that there is high chance that you'll lose data precision, thus in short is like telling the compiler "go ahead and convert it as I don't mind data loss" , so, no exception will be thrown whereas the last example that converts from string
to int
should throw an exception because according to MSDN: 您可以将当前用于将double
转换为int
的实用程序方法视为“转换”即(int)12.4
, (int)12.44
等等,这实际上意味着您确定知道您很有可能ll会丢失数据精度,因此简而言之就是告诉编译器“继续将其转换为我不介意数据丢失” ,因此,不会抛出任何异常,而最后一个从string
转换为int
示例应抛出异常因为根据MSDN:
ToInt32(String) method is equivalent to passing value to the Int32.Parse(String). ToInt32(String)方法相当于将值传递给Int32.Parse(String)。
and as we all know Int32.Parse(String)
throws an exception if the specified string is not in the correct format. 并且我们都知道如果指定的字符串格式不正确, Int32.Parse(String)
会抛出异常。
You first have to convert your string into double, then cast it to int. 首先必须将字符串转换为double,然后将其转换为int。 or do another convert to int. 或者做另一个转换为int。
result = Convert.ToInt32(Convert.ToDouble("12.4"));
From msdn Convert.ToInt32(string)
来自msdn Convert.ToInt32(string)
Converts the specified string representation of a number to an equivalent 32-bit signed integer . 将指定的数字字符串表示形式转换为等效的32位有符号整数 。
https://msdn.microsoft.com/en-us/library/sf1aw27b(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/sf1aw27b(v=vs.110).aspx
In given examples you can see that converting from double representation to int gives format exception. 在给定的示例中,您可以看到从double表示转换为int会给出格式异常。
so this is clearly by design. 所以这显然是设计的。 you should do it right. 你应该做对。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.