Avatar of AlHal2
AlHal2
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Atof Equivalent in C#

Is it possible to convert this code to C#?


	int pos=0,p=0;
	double v1,expo,res;

	p=value.find("e+");
	
	if(value.find("e+")!=-1 ||value.find("e-")!=-1)
	{
		pos=value.find("e");
		if(atof(value.substr(0,pos).c_str()))
		{
		v1=StringToDouble(value.substr(0,pos));
		expo=StringToDouble(value.substr(pos+1,(value.length()-(pos+1))));
		res=pow(10,expo)*v1;
		return DoubleToString(res);


		}
	}

Open in new window


I saw this online, but wasn't sure how to get the Takewhile function to work.
https://msdn.microsoft.com/en-us/library/system.linq.enumerable.takewhile(v=vs.100).aspx

float myAtof(string myString)
{
    Predicate<char> testChar = c => c == '.' || 
                                    c == '-' || 
                                    c == '+' || 
                                    char.IsDigit(c);

    myString = new string(myString.Trim().TakeWhile(testChar).ToArray());

    if (myString.Length > 0)
    {
        float rvl;

        // accounts for bogus strings of valid chars, e.g. ".-":
        if (float.TryParse(myString, out rvl))
        {
            return rvl;
        }
    }

    return 0;
}

Open in new window

C#

Avatar of undefined
Last Comment
AlHal2

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
it_saige

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
AlHal2

ASKER
Thanks.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy