Java Utililty Methods Float to String

List of utility methods to do Float to String

Description

The list of methods to do Float to String are organized into topic(s).

Method

StringfloatToFormattedString(float f)
Shortens numbers to a representable state (#*.###)
if (f == (int) f) {
    return String.valueOf((int) f);
} else {
    return new DecimalFormat("#.##").format(f);
StringfloatToString(final float value, final boolean stripDotZero)
Converts a given float value to a String with a single digit after that decimal point and optionally strips ".0" if present.
return fixedPointToString((long) (value * 10), stripDotZero);
StringfloatToString(float boost)
float To String
if (boost != 1.0f)
    return "^" + Float.toString(boost);
return "";
StringfloatToString(float d)
float To String
if (d == (long) d) 
    return String.format("%d", (long) d);
else {
    d = Math.round(d * 100);
    d = d / 100;
    String s = String.valueOf(d);
    return s.contains(".") ? s.replaceAll("0*$", "").replaceAll("\\.$", "") : s;
StringfloatToString(float f, boolean asAPI)
Converts a float to a string and is removing the ".0" if the float is an integer.
String valS = String.valueOf(f);
return valS.endsWith(".0") ? valS.substring(0, valS.length() - 2) : (valS + (asAPI ? "f" : ""));
StringfloatToString(float f, int precision)
float To String
boolean negative = f < 0;
f = (negative ? -f : f);
long whole_rep = Math.round(f * Math.pow(10, -precision));
String wholeRepStr = String.valueOf(whole_rep);
if (whole_rep == 0)
    return wholeRepStr;
int whole_len = wholeRepStr.length();
int pre_len = whole_len + precision;
...
StringfloatToString(float f, int precision)
Convert float to string, with given precision.
String s = Float.toString(f);
int i = s.lastIndexOf('.');
if (i == -1)
    return s;
int end = i + precision + 1;
return (end < s.length()) ? s.substring(0, end) : s;
StringfloatToString(float fValue)
Converts a float value to a String value
String value = String.valueOf(fValue);
value = value.replaceFirst("\\.*0*$", "");
return value;
StringfloatToString(Float num)
float To String
java.text.NumberFormat f = java.text.NumberFormat.getInstance();
f.setGroupingUsed(false);
return f.format(num);
StringfloatToString(float val, int width)
float To String
String str = mDF.format(val);
if (str.length() >= width) {
    return str;
return mSpaces.substring(0, width - str.length()) + str;