HCL America Interview Question for Solutions Architects






Comment hidden because of low score. Click to expand.
0
of 0 vote

public static double StringToFloat(string s)
        {
            if (s == null || s.Length == 0)
                throw new InvalidStringException("Invalid String Entered");
            int length = s.Length;
            int i = 0;
            double lastNumber = 0;
            int returnNumber = 0;
            bool numberNegative = false;
            bool numberPositive = false;
            int startPoint = 0;
            int factor = 1;
            bool IsFloat = false;
            if (s[0] == '-')
            {
                numberNegative = true;
                startPoint = 1;
            }
            if (s[0] == '+')
            {
                numberPositive = true;
                startPoint = 1;
            }
            for (i = startPoint; i < length; i++)
            {
                if (s[i] == ' ')
                {
                    continue;
                }
                else if (s[i] == '.')
                {
                    IsFloat = true;
                    continue;
                }
                else
                {
                    if ((s[i] >= '0') && s[i] <= '9')
                    {
                        returnNumber = s[i] - '0';
                        if (i > 0) lastNumber = lastNumber * 10;
                        lastNumber = lastNumber + returnNumber;
                        if (IsFloat)
                            factor *= 10;
                    }
                    else
                    {
                        break;
                    }

                }
            }
            if (numberNegative)
                lastNumber = -1 * lastNumber;

            if (IsFloat)
                lastNumber = lastNumber / factor;

            return lastNumber;
        }

- manu November 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This code will work however for numbers with high precision (PI to the 20th digit) it will throw an exception since it builds an int and divides by a factor at the end.

- Reza August 22, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

java
// In Java
class StrToFloat {
    public static void main(String[] args) {
        String str = "1234.56";
        float n = 0;
        int intPartLength = str.length() - str.substring(str.indexOf('.')).length();
        for(int i = 0; i < intPartLength; i++) {
            n += (str.charAt(i) - '0') * Math.pow(10, (intPartLength - 1) - i);
        }
        for(int i = intPartLength+1; i < str.length(); i++) {
            n += (str.charAt(i) - '0') / Math.pow(10, (i - intPartLength+1)-1);
        }

        System.out.println(n);
    }
}

- Phil Fernandez October 27, 2020 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More