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?
By default,
double.TryParse
uses the following flags fromNumberStyles
: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) theAllowExponent
flag.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.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.
It returns true because it sees it as scientific notation, as noted here:
The easiest way is to probably just check if the string contains the letter
e
: