Java Float to String floatToString(final float value, final boolean stripDotZero)

Here you can find the source of floatToString(final float value, final boolean stripDotZero)

Description

Converts a given float 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 floatToString(final float value, final boolean stripDotZero) 

Method Source Code

//package com.java2s;
/*/* ww  w .j  ava2  s .com*/
 * 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 float value to a String with a single digit after that
     * decimal point and optionally strips ".0" if present.
     */
    public static String floatToString(final float 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. floatToFormattedString(float f)
  2. floatToString(float boost)
  3. floatToString(float d)
  4. floatToString(float f, boolean asAPI)
  5. floatToString(float f, int precision)