Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find string value is in exponential format in C#?

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?

like image 661
Selvamz Avatar asked Jun 12 '15 01:06

Selvamz


2 Answers

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);
}
like image 75
sstan Avatar answered Sep 22 '22 14:09

sstan


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);
like image 38
nevermoi Avatar answered Sep 22 '22 14:09

nevermoi