Android Utililty Methods Float Format

List of utility methods to do Float Format

Description

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

Method

StringformatAmount(float f)
12->12.00 or 11111.1 -> 1,111.10
try {
    DecimalFormat df = new DecimalFormat("###,###.00");
    return df.format(f);
} catch (Exception e) {
    return "";
StringformatFloatValue(float value)
format Float Value
if (value <= 0.0f) {
    return "0";
String str = String.valueOf(value);
Log.d("", " formatFloatValue" + str);
int index = str.indexOf('.');
if (index < 0) {
    return str;
...
StringformatFloat(float money)
format Float
return new DecimalFormat("0.00").format(money);
StringformatAmount(float f)
12->12.00 or 11111.1 -> 1,111.10
try {
    DecimalFormat df = new DecimalFormat("###,###.00");
    return df.format(f);
} catch (Exception e) {
    return "";
StringformatSpeed(float data)
format Speed
return formatSpeed(data, "#0.00");
StringformatSpeed(float data, String format)
format Speed
java.text.DecimalFormat df = new java.text.DecimalFormat(format);
if (data < 0)
    return "0B/s";
if (data < 1000.0) {
    if (data == (int) data) {
        df = new java.text.DecimalFormat("#0");
    return df.format(data) + "KB/s";
...
StringformatSpeedValue(float speed)
format Speed Value
java.text.DecimalFormat df = new java.text.DecimalFormat("#0.00");
String str = "";
if (speed >= 0 && speed < 1024.0) {
    df = new java.text.DecimalFormat("#0");
    str = df.format(speed) + "KB/s";
} else if (speed >= 1024 && speed < (1024 * 1024.0)) {
    str = df.format(speed / 1024) + "MB/s";
} else if (speed >= (1024 * 1024) && speed < (1024 * 1024 * 1024.0)) {
...
MapformatSpeedWithUnit(float speed)
format Speed With Unit
java.text.DecimalFormat df = new java.text.DecimalFormat("#0.00");
Map<String, String> map = new HashMap<String, String>();
if (speed < 0) {
    map.put("speed", "0");
    map.put("unit", "KB/s");
    return map;
if (speed < 1000.0) {
...
StringformatSpeed(float bytesPerSecond)
format Speed
float bitsPerSecond = bytesPerSecond * 8;
int unit = 1000;
if (bitsPerSecond < unit)
    return bitsPerSecond + " bits/sec";
int exp = (int) (Math.log(bitsPerSecond) / Math.log(unit));
String pre = String.valueOf("kmgtpe".charAt(exp - 1));
return String.format("%.1f %sB/sec",
        bitsPerSecond / Math.pow(unit, exp), pre);
...
StringpriceFormat(float price, String pattern)
price Format
String str = "0";
double anotherNan = Double.NaN;
if (Double.compare(price, anotherNan) != 0) {
    DecimalFormat df1 = new DecimalFormat(pattern);
    str = df1.format(price);
} else {
    DecimalFormat df1 = new DecimalFormat(pattern);
    str = df1.format(str);
...