I want to find whether string has exponential format or not. Currently i am checking like below.
var s = "1.23E+04";
var hasExponential = s.Contains("E");
But i know this is not the proper way. So can anyone please guide proper and fastest way to achieve my requirement?
If you also want to make sure it really is a number, not just a string with an 'E' in it, maybe a function like this can be helpful. The logic remains simple.
private bool IsExponentialFormat(string str)
{
double dummy;
return (str.Contains("E") || str.Contains("e")) && double.TryParse(str, out dummy);
}
Try to use regular expression?
string s = "1.23E+04";
string pattern = @"^\d{1}.\d+(E\+)\d+$";
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
bool hasExponential = rgx.IsMatch(s);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With