为什么double.TryParse(“6E02”,out tempDouble)返回true?

为什么double.TryParse(“6E02”,out tempDouble)返回true?

问题描述:

我花了一天时间来弄清楚一个if语句对于字符串值返回true的问题。

It took me a day to figure out the problem that one of the if statement returns true for a string value.

我们正在解析以检查值是否为数字或字符串。我发现这个语句使用,当字符串值作为6E02时,语句返回true,这是一个双重值。

We are parsing to check whether the value is a number or a string. I found out that this statement used and when the string value comes in as 6E02 the statement return true that this is a double value.

var double temp;
var val ="6E02"
result = double.TryParse(val, out temp)

如何解决这个问题,为(Number)E0(Number)

How can I fix this issue to return the result false for strings like (Number)E0(Number)

相信首先检查文本是否包含E0,如果它只返回false。但是有没有更好的方法来处理这个或另一个内置的方法来替换方法?

Easy way I believe to check the text first if it contains E0 and if it does just return false. But is there a better way of handling this or another built in method to replace the method with?

默认情况下, double.TryParse 使用以下标志从 NumberStyles

By default, double.TryParse uses the following flags from NumberStyles:


  • NumberStyles.AllowThousands

  • NumberStyles.Float,它是以下组合的别名:

    • NumberStyles.AllowLeadingWhite

    • NumberStyles.AllowTrailingWhite

    • NumberStyles.AllowLeadingSign

    • NumberStyles.AllowDecimalPoint

    • NumberStyles.AllowExponent

    • NumberStyles.AllowThousands
    • NumberStyles.Float, which is an alias for the following combination:
      • NumberStyles.AllowLeadingWhite
      • NumberStyles.AllowTrailingWhite
      • NumberStyles.AllowLeadingSign
      • NumberStyles.AllowDecimalPoint
      • NumberStyles.AllowExponent

      您可以使用其他超负荷 TryParse 仅指定您喜欢的一部分。特别是,您要删除(至少) AllowExponent 标志。