-5

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

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)

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

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?

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
akd
  • 6,538
  • 16
  • 70
  • 112
  • 1
    I'm thinking parsing for e's first is probably your best option, since 6E02 is a valid representation of a double. – Kevin DiTraglia Oct 17 '14 at 15:13
  • 3
    Are you aware of exponential notation? – Sriram Sakthivel Oct 17 '14 at 15:14
  • 1
    I assume your title is wrong. `E02` will not return true, but `6E02` will, as per @Andrew's answer – DavidG Oct 17 '14 at 15:14
  • 6 x 10 ^ 2 or 6E2 is a valid double. – PJRobot Oct 17 '14 at 15:14
  • 6E02 and any Ex or E-x is a valid representation of a number. It's called scientific notation or exponential notation. 6E02 = 6 x 10^2 = 600 – thorkia Oct 17 '14 at 15:15
  • Yes I am aware of those number totally . but if you look up on the internet with the question how to check if is a text is a number or not you come cross with this https://social.msdn.microsoft.com/Forums/windows/en-US/84990ad2-5046-472b-b103-f862bfcd5dbc/how-to-check-string-is-number-or-not-in-c?forum=winforms and no one mentions the exception of 6E02 – akd Oct 17 '14 at 15:17
  • @tnw, http://meta.stackexchange.com/questions/23628/how-should-we-deal-with-rtfm-comments – Habib Oct 17 '14 at 15:28

3 Answers3

13

6E02 is scientific notation for 6*10^2 or 600, which is certainly a double. This is built into C#.

If you want to exclude numbers with scientific notation, there is an overload to TryParse that has several options, one of which is whether to include scientific notation or not.

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

This example takes no styles, which means only strings with digits will be parsed. There are other options you can include as mentioned in Sam's answer.

You also have to specify a culture with this overload; my example uses the app's current culture, but you can explicitly give it whatever you want.

Andrew
  • 4,953
  • 15
  • 40
  • 58
  • How about if there is a dot in the string ? example 1.2 or comma 1,2 ? – akd Oct 17 '14 at 15:42
  • Use `NumberStyles.AllowDecimalPoint`. The culture should handle whether the decimal point is a period or comma. – Andrew Oct 17 '14 at 16:04
10

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

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

You can use the other overload of TryParse to specify only a subset of these to your liking. In particular, you want to remove (at least) the AllowExponent flag.

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
3

It returns true because it sees it as scientific notation, as noted here:

An uppercase or lowercase character 'e', that indicates exponential (scientific) notation.

The easiest way is to probably just check if the string contains the letter e:

if(val.ToLower().Contains("e"))
{
    //Remove the letter, or parse it in a different way.
}
Dave Zych
  • 21,581
  • 7
  • 51
  • 66
  • 1
    Simplest way is not to use `NumberStyles.AllowExponent`. :p – Sriram Sakthivel Oct 17 '14 at 15:19
  • @SriramSakthivel I do agree, however it kind of depends on what he wants to do. If it's scientific notation, he might not want to parse it at all in which case the `NumberStyles` doesn't come into play. – Dave Zych Oct 17 '14 at 16:31