I was asked this on an interview a while back and couldn't figure it out. I wasn't allowed to cast the entire thing at once so my next idea was to just run through the string converting until the point but the guy interviewing me told me he wanted to do something like this:
1 = 1
12 = 1 * 10 + 2
123 = 12 * 10 + 3
1234 = 123 * 10 + 4
The input is convert "1234.567" to a float ie. 1234.567
I honestly have no idea how he meant to do it and I haven't been able to produce good enough code to show you guys all I had was the for cycling to parse each character:
for(int i = 0; i < str.length(); i++){
if(!str.charAt(i).equals(".")){
fp = Float.parseFloat("" + str.charAt(i));
charto anintbyint i = c - '0'. The rest pretty much follows his explanation but converted to code!str.charAt(i).equals(".")<-- this will not compile since.charAt()returns acharand primitive types do not support.equals(); whatever,"."is aString, not achar. You wantstr.charAt(i) != '.'.