C# Convert.ToDecimal(string) & Decimal.Parse(string) 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/89203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between Convert.ToDecimal(string) & Decimal.Parse(string)
提问by YonahW
What is the difference in C# between Convert.ToDecimal(string)
and Decimal.Parse(string)
?
C#Convert.ToDecimal(string)
和之间有什么区别Decimal.Parse(string)
?
In what scenarios would you use one over the other?
在什么情况下你会使用一个?
What impact does it have on performance?
它对性能有什么影响?
What other factors should I be taking into consideration when choosing between the two?
在两者之间进行选择时,我还应该考虑哪些其他因素?
采纳答案by Guy Starbuck
From bytes.com:
来自bytes.com:
The Convert class is designed to convert a wide range of Types, so you can convert more types to Decimal than you can with Decimal.Parse, which can only deal with String. On the other hand Decimal.Parse allows you to specify a NumberStyle.
Decimal and decimal are aliases and are equal.
For Convert.ToDecimal(string), Decimal.Parse is called internally.
Morten Wennevik [C# MVP]
Convert 类旨在转换范围广泛的类型,因此与 Decimal.Parse 相比,您可以将更多类型转换为 Decimal,而 Decimal.Parse 只能处理 String。另一方面,Decimal.Parse 允许您指定 NumberStyle。
Decimal 和 decimal 是别名并且是相等的。
对于 Convert.ToDecimal(string),内部调用 Decimal.Parse。
Morten Wennevik [C# MVP]
Since Decimal.Parse is called internally by Convert.ToDecimal, if you have extremeperformance requirements you might want to stick to Decimal.Parse, it will save a stack frame.
由于 Decimal.Parse 由 Convert.ToDecimal 内部调用,如果您有极端的性能要求,您可能希望坚持使用 Decimal.Parse,它将保存一个堆栈帧。
回答by dimarzionist
One common suggestion related to original topic - please use TryParse()
as soon as you not really sure that input string parameter WILL be correct number format representation.
与原始主题相关的一个常见建议 -TryParse()
如果您不确定输入字符串参数将是正确的数字格式表示,请立即使用。
回答by David J. Sokol
One factor that you might not have thought of is the Decimal.TryParse
method. Both Convert.ToDecimal
and Parse
throw exceptions if they cannot convert the string to the proper decimal format. The TryParse method gives you a nice pattern for input validation.
您可能没有想到的一个因素是Decimal.TryParse
方法。双方Convert.ToDecimal
并Parse
抛出异常,如果他们不能将字符串转换为正确的十进制格式。TryParse 方法为您提供了一个很好的输入验证模式。
decimal result;
if (decimal.TryParse("5.0", out result))
; // you have a valid decimal to do as you please, no exception.
else
; // uh-oh. error message time!
This pattern is very incredibly awesome for error-checking user input.
这种模式对于检查用户输入的错误非常棒。
回答by James Newton-King
There is one important difference to keep in mind:
要记住一个重要的区别:
Convert.ToDecimal
will return 0
if it is given a null
string.
Convert.ToDecimal
0
如果给定一个null
字符串,它将返回。
decimal.Parse
will throw an ArgumentNullException
if the string you want to parse is null
.
decimal.Parse
将抛出ArgumentNullException
,如果你想解析字符串null
。
回答by Taran
Major Difference between Convert.ToDecimal(string)
and Decimal.Parse(string)
is that Convert handles Null
whereas the other throws an exception
Convert.ToDecimal(string)
和之间的主要区别Decimal.Parse(string)
是 Convert 处理Null
而另一个抛出异常
Note: It won't handle empty string.
注意:它不会处理空字符串。
回答by tony95
Convert.ToDecimal apparently does not always return 0. In my linq statement
Convert.ToDecimal 显然并不总是返回 0。在我的 linq 语句中
var query = from c in dc.DataContext.vw_WebOrders
select new CisStoreData()
{
Discount = Convert.ToDecimal(c.Discount)
};
Discount is still null after converting from a Decimal? that is null. However, outside a Linq statement, I do get a 0 for the same conversion. Frustrating and annoying.
从十进制转换后折扣仍然为空?那是空的。但是,在 Linq 语句之外,对于相同的转换,我确实得到了 0。令人沮丧和烦人。
回答by Nandostyle
Knowing that Convert.ToDecimal is the way to go in most cases because it handles NULL, it, however, does not handle empty string very well. So, the following function might help:
知道 Convert.ToDecimal 在大多数情况下是可行的方法,因为它处理 NULL,但是它不能很好地处理空字符串。因此,以下功能可能会有所帮助:
'object should be a string or a number
Function ConvertStringToDecimal(ByVal ValueToConvertToDecimal As Object) As Decimal
If String.IsNullOrEmpty(ValueToConvertToDecimal.ToString) = False Then
Return Convert.ToDecimal(ValueToConvertToDecimal)
Else
Return Convert.ToDecimal(0)
End If
End Function