## 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;
}``````

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

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.

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);
}
}``````

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.

### 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.