Java Double to String doubleToString(final double value, final boolean stripDotZero)

Here you can find the source of doubleToString(final double value, final boolean stripDotZero)

Description

Converts a given double value to a String with a single digit after that decimal point and optionally strips ".0" if present.

License

Open Source License

Declaration

public static String doubleToString(final double value, final boolean stripDotZero) 

Method Source Code

//package com.java2s;
/*//from www.j  a v  a 2  s. co  m
 * Copyright 2007 Joachim Sauer
 * Copyright 2002-2006 Chriss Veness (vincenty formula in distance())
 * 
 * This file is part of bbTracker.
 * 
 * bbTracker is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * bbTracker is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Converts a given double value to a String with a single digit after that
     * decimal point and optionally strips ".0" if present.
     */
    public static String doubleToString(final double value, final boolean stripDotZero) {
        return fixedPointToString((long) (value * 10), stripDotZero);
    }

    private static String fixedPointToString(final long value, final boolean stripDotZero) {
        final String string = String.valueOf(value);
        final int stringLength = string.length();
        final StringBuffer result = new StringBuffer(stringLength + 1);
        result.append(string.substring(0, stringLength - 1));
        if (result.length() == 0) {
            result.append("0");
        }
        if (!(stripDotZero && string.endsWith("0"))) {
            result.append('.');
            result.append(string.charAt(stringLength - 1));
        }
        return result.toString();
    }
}

Related

  1. doubleToString(double[] values)
  2. doubleToString(final double d)
  3. doubleToString(final double v, final int roundingDigits)
  4. doubleToString(final Double value)
  5. doubleToString(final Double value)
  6. doubleToString4(double value)
  7. doubleToStringWithMinimumPrecision(double d)