Here you can find the source of parseDecimal(PushbackReader reader)
public static double parseDecimal(PushbackReader reader) throws IOException, ParseException
//package com.java2s; /*//from www . j ava2 s .c o m * (c) Copyright 2007 by Volker Bergmann. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, is permitted under the terms of the * GNU General Public License. * * For redistributing this software or a derivative work under a license other * than the GPL-compatible Free Software License as defined by the Free * Software Foundation or approved by OSI, you must first obtain a commercial * license to this software product from Volker Bergmann. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS, * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ import java.io.PushbackReader; import java.io.IOException; import java.text.ParseException; import java.text.ParsePosition; public class Main { public static double parseDecimal(PushbackReader reader) throws IOException, ParseException { double result = parseInteger(reader); double postfix = parseOptionalPostfix(reader); if (result >= 0) result += postfix; else result -= postfix; return result; } public static long parseInteger(PushbackReader reader) throws IOException, ParseException { boolean negative = parseOptionalSign(reader); return parseNonNegativeInteger(reader) * (negative ? -1 : 1); } public static double parseOptionalPostfix(PushbackReader reader) throws IOException { int c = reader.read(); if (c != '.') { if (c != -1) reader.unread(c); return 0.; } double result = 0; double base = 0.1; while ((c = reader.read()) != -1 && Character.isDigit((char) c)) { result += (c - '0') * base; base *= 0.1; } if (c != -1) reader.unread(c); return result; } public static boolean parseOptionalSign(PushbackReader reader) throws IOException { skipWhitespace(reader); int optionalSign = reader.read(); if (optionalSign == '-') return true; if (optionalSign != -1) reader.unread(optionalSign); return false; } public static long parseNonNegativeInteger(String source, ParsePosition pos) throws ParseException { int digit; if (pos.getIndex() > source.length() || !Character.isDigit(digit = source.charAt(pos.getIndex()))) throw new ParseException("Number expected", 0); pos.setIndex(pos.getIndex() + 1); long result = digit - '0'; while (pos.getIndex() < source.length() && Character.isDigit(digit = source.charAt(pos.getIndex()))) { result = result * 10 + digit - '0'; pos.setIndex(pos.getIndex() + 1); } return result; } public static long parseNonNegativeInteger(PushbackReader reader) throws IOException, ParseException { int digit; if ((digit = reader.read()) == -1 || !Character.isDigit((char) digit)) throw new ParseException("Long expected", 0); long result = digit - '0'; while ((digit = reader.read()) != -1 && Character.isDigit((char) digit)) result = result * 10 + digit - '0'; if (digit != -1) reader.unread(digit); return result; } public static void skipWhitespace(PushbackReader reader) throws IOException { int c; do { c = reader.read(); } while (c != -1 && Character.isWhitespace((char) c)); if (c != -1) reader.unread(c); } public static void skipWhiteSpace(String text, ParsePosition pos) { int i; while ((i = pos.getIndex()) < text.length() && Character.isWhitespace(text.charAt(i))) pos.setIndex(i + 1); } }