static Object js_parseFloat ( Object [ ] args ) { if ( args . length < 1 ) return ScriptRuntime . NaNobj ; String s = ScriptRuntime . toString ( args [ 0 ] ) ; int len = s . length ( ) ; int start = 0 ; // Scan forward to skip whitespace char c ; for ( ; ; ) { if ( start == len ) { return ScriptRuntime . NaNobj ; } c = s . charAt ( start ) ; if ( ! ScriptRuntime . isStrWhiteSpaceChar ( c ) ) { break ; } ++ start ; } int i = start ; if ( c == ' ' || c == ' ' ) { ++ i ; if ( i == len ) { return ScriptRuntime . NaNobj ; } c = s . charAt ( i ) ; } if ( c == ' ' ) { // check for "Infinity" if ( i + 8 <= len && s . regionMatches ( i , "Infinity" , 0 , 8 ) ) { double d ; if ( s . charAt ( start ) == ' ' ) { d = Double . NEGATIVE_INFINITY ; } else { d = Double . POSITIVE_INFINITY ; } return ScriptRuntime . wrapNumber ( d ) ; } return ScriptRuntime . NaNobj ; } // Find the end of the legal bit int decimal = - 1 ; int exponent = - 1 ; boolean exponentValid = false ; for ( ; i < len ; i ++ ) { switch ( s . charAt ( i ) ) { case ' ' : if ( decimal != - 1 ) // Only allow a single decimal point. break ; decimal = i ; continue ; case ' ' : case ' ' : if ( exponent != - 1 ) { break ; } else if ( i == len - 1 ) { break ; } exponent = i ; continue ; case ' ' : case ' ' : // Only allow '+' or '-' after 'e' or 'E' if ( exponent != i - 1 ) { break ; } else if ( i == len - 1 ) { -- i ; break ; } continue ; case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : if ( exponent != - 1 ) { exponentValid = true ; } continue ; default : break ; } break ; } if ( exponent != - 1 && ! exponentValid ) { i = exponent ; } s = s . substring ( start , i ) ; try { return Double . valueOf ( s ) ; } catch ( NumberFormatException ex ) { return ScriptRuntime . NaNobj ; } }
The global method parseFloat as per ECMA - 262 15 . 1 . 2 . 3 .