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

AlHal2Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
In C#, we use the inbuilt Convert Methods, e.g. -
using System;

namespace EE_Q29069400
{
    class Program
    {
        static void Main(string[] args)
        {
            string factorial = "7.1099859e+74";
            double converted = Convert.ToDouble(factorial);
            Console.WriteLine($"Converted string - {factorial}; to Double - {converted}");
            Console.ReadLine();
        }
    }
}

Open in new window

You can also use the type specific Parse and/or TryParse methods; e.g. -
using System;

namespace EE_Q29069400
{
    class Program
    {
        static void Main(string[] args)
        {
            string factorial = "7.1099859e+74";
            double converted;
            if (double.TryParse(factorial, out converted))
            {
                Console.WriteLine($"Converted string - {factorial}; to Double - {converted}");
            }
            else
            {
                Console.WriteLine($"Could not convert {factorial}");
            }
            Console.ReadLine();
        }
    }
}

Open in new window


Each of the preceeding produce the following output -Capture.PNG
-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AlHal2Author Commented:
Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.