public Number doParse ( String text , ParsePosition parsePosition , double baseValue , double upperBound , boolean lenientParse ) { // we don't have to do anything special to do the parsing here, // but we have to turn lenient parsing off-- if we leave it on, // it SERIOUSLY messes up the algorithm // if withZeros is true, we need to count the zeros // and use that to adjust the parse result int zeroCount = 0 ; if ( withZeros ) { String workText = text ; ParsePosition workPos = new ParsePosition ( 1 ) ; //int digit; while ( workText . length ( ) > 0 && workPos . getIndex ( ) != 0 ) { workPos . setIndex ( 0 ) ; /*digit = */ ruleSet . parse ( workText , workPos , 1 ) . intValue ( ) ; // parse zero or nothing at all if ( workPos . getIndex ( ) == 0 ) { // we failed, either there were no more zeros, or the number was formatted with digits // either way, we're done break ; } ++ zeroCount ; parsePosition . setIndex ( parsePosition . getIndex ( ) + workPos . getIndex ( ) ) ; workText = workText . substring ( workPos . getIndex ( ) ) ; while ( workText . length ( ) > 0 && workText . charAt ( 0 ) == ' ' ) { workText = workText . substring ( 1 ) ; parsePosition . setIndex ( parsePosition . getIndex ( ) + 1 ) ; } } text = text . substring ( parsePosition . getIndex ( ) ) ; // arrgh! parsePosition . setIndex ( 0 ) ; } // we've parsed off the zeros, now let's parse the rest from our current position Number result = super . doParse ( text , parsePosition , withZeros ? 1 : baseValue , upperBound , false ) ; if ( withZeros ) { // any base value will do in this case. is there a way to // force this to not bother trying all the base values? // compute the 'effective' base and prescale the value down long n = result . longValue ( ) ; long d = 1 ; while ( d <= n ) { d *= 10 ; } // now add the zeros while ( zeroCount > 0 ) { d *= 10 ; -- zeroCount ; } // d is now our true denominator result = new Double ( n / ( double ) d ) ; } return result ; }
Dispatches to the inherited version of this function but makes sure that lenientParse is off .