Java Float to String floatToFormattedString(float f)

Here you can find the source of floatToFormattedString(float f)

Description

Shortens numbers to a representable state (#*.###)

License

Open Source License

Parameter

Parameter Description
f float to trim

Return

trimmed number as a string

Declaration

public static String floatToFormattedString(float f) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.DecimalFormat;

public class Main {
    /**/*  w  ww .  java 2  s  .c o m*/
     * Shortens numbers to a representable state (#*.###)
     * @param f float to trim
     * @return trimmed number as a string
     */
    public static String floatToFormattedString(float f) {
        if (f == (int) f) {
            return String.valueOf((int) f);
        } else {
            return new DecimalFormat("#.##").format(f);
        }
    }
}

Related

  1. floatToString(final float value, final boolean stripDotZero)
  2. floatToString(float boost)
  3. floatToString(float d)
  4. floatToString(float f, boolean asAPI)