Java Float to String floatToString(float value, int precision)

Here you can find the source of floatToString(float value, int precision)

Description

Converts a float to a string.

License

Open Source License

Parameter

Parameter Description
value value
precision precision

Return

a string

Declaration

public static String floatToString(float value, int precision) 

Method Source Code

//package com.java2s;
/*//  w w w.j  a  v  a2  s .  com
 * Java
 *
 * Copyright 2016 IS2T. All rights reserved.
 * Use of this source code is subject to license terms.
 */

public class Main {
    /**
     * Converts a float to a string.
     *
     * @param value
     *            value
     * @param precision
     *            precision
     * @return a string
     */
    public static String floatToString(float value, int precision) {
        String str = String.valueOf(value);
        int dot = str.indexOf('.');
        if (precision > 0) {
            precision++;
        }
        if (dot > 0 && str.length() > dot + precision) {
            str = str.substring(0, dot + precision);
        }
        return str;
    }
}

Related

  1. floatToString(float fValue)
  2. floatToString(Float num)
  3. floatToString(float val, int width)
  4. FloatToString(float value)
  5. floatToString(float value)
  6. floatToString(float value, String format)
  7. floatToString(float[] values)
  8. floatToString0(float value)