这有点令人困惑,为什么会这样? Console.WriteLine(IsNumeric("$0")) - FalseConsole.WriteLine(IsNumeric("€0")) - FalseConsole.WriteLine(IsNumeric("£0")) - TrueConsole.WriteLine(IsNumeric("£")) - False 对我来说似乎很奇怪
Console.WriteLine(IsNumeric("$0")) - False Console.WriteLine(IsNumeric("€0")) - False Console.WriteLine(IsNumeric("£0")) - True Console.WriteLine(IsNumeric("£")) - False
对我来说似乎很奇怪,它是什么原因造成的
编辑:也在最后工作:
Console.WriteLine(IsNumeric("300£")) - True Console.WriteLine(IsNumeric("300£0")) - False Console.WriteLine(IsNumeric("£300£")) - False住在意大利 – 在意大利语中运行
Console.WriteLine(IsNumeric("$0")) ' False Console.WriteLine(IsNumeric("€0")) ' True Console.WriteLine(IsNumeric("£0")) ' False Console.WriteLine(IsNumeric("£")) ' False
虽然这会让你永远回归真实
using System.Globalization; Console.WriteLine(MyIsNumeric("$0", NumberStyles.AllowCurrencySymbol, "en-US")) Console.WriteLine(MyIsNumeric("€0", NumberStyles.AllowCurrencySymbol, "it-IT")) Console.WriteLine(MyIsNumeric("£0", NumberStyles.AllowCurrencySymbol, "en-GB")) Public Function MyIsNumeric(ByVal val as String, ByVal NumberStyle as NumberStyles, cName as String) as Boolean Dim result as Double return Double.TryParse(val,NumberStyle, new CultureInfo(cName), result) End Function
因此,我们可以得出结论,IsNumeric(最初在VB6运行时中定义)足够智能,可以在解析当前区域设置货币符号(如果它们位于输入字符串之前或之后)时将其排除.