public static double atof ( String s ) { int i = 0 ; int sign = 1 ; double r = 0 ; // integer part double p = 1 ; // exponent of fractional part int state = 0 ; // 0 = int part, 1 = frac part while ( i < s . length ( ) && Character . isWhitespace ( s . charAt ( i ) ) ) i ++ ; if ( i < s . length ( ) && s . charAt ( i ) == ' ' ) { sign = - 1 ; i ++ ; } else if ( i < s . length ( ) && s . charAt ( i ) == ' ' ) { i ++ ; } while ( i < s . length ( ) ) { char ch = s . charAt ( i ) ; if ( ' ' <= ch && ch <= ' ' ) { if ( state == 0 ) r = r * 10 + ch - ' ' ; else if ( state == 1 ) { p = p / 10 ; r = r + p * ( ch - ' ' ) ; } } else if ( ch == ' ' ) { if ( state == 0 ) state = 1 ; else return sign * r ; } else if ( ch == ' ' || ch == ' ' || ch == ' ' || ch == ' ' ) { long e = ( int ) parseLong ( s . substring ( i + 1 ) , 10 ) ; return sign * r * Math . pow ( 10 , e ) ; } else return sign * r ; i ++ ; } return sign * r ; }
Converts a string of digits to an double .