为什么double.TryParse(“6E02”,out tempDouble)返回true? [英] Why does double.TryParse("6E02", out tempDouble) return true?

查看:355
本文介绍了为什么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 使用以下标志从< a href =http://msdn.microsoft.com/en-us/library/system.globalization.numberstyles.aspx> 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 标志。

      这篇关于为什么double.TryParse(“6E02”,out tempDouble)返回true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆