Example usage for java.lang String format

List of usage examples for java.lang String format

Introduction

In this page you can find the example usage for java.lang String format.

Prototype

public static String format(String format, Object... args) 

Source Link

Document

Returns a formatted string using the specified format string and arguments.

Usage

From source file:Main.java

public static String toHexString(final byte[] src) {
    StringBuffer buf = new StringBuffer();
    for (byte b : src) {
        String s = String.format("%02x", b & 0xff);
        buf.append(s).append(",");
    }//from   w  w w. j a v a 2 s.c o  m
    return buf.toString();
}

From source file:Main.java

static void checkAngle(int angle) {
    if (angle < 0 || angle > 360)
        throw new IllegalArgumentException(String.format("Illegal angle %d: must be >=0 and <= 360", angle));
}

From source file:Main.java

/**
 * Convert the color value to hex//w w  w.  j a  v  a 2  s . co  m
 * @param color
 * @return hex string
 */
public static String colorToHex(int color) {
    return String.format("#%06X", (0xFFFFFF & color));
}

From source file:Main.java

public static String getFormattedColorString(int color, boolean showAlpha) {
    if (showAlpha)
        return String.format("#%08X", color);
    else/*from  ww w .j  av  a 2s .  c  om*/
        return String.format("#%06X", 0xFFFFFF & color);
}

From source file:Main.java

public static String getDataDirectory(Context context) {
    String dirname = String.format("/data/data/%s/", context.getApplicationContext().getPackageName());
    return dirname;
}

From source file:Main.java

public static String fileSizeFormat(long size) {
    float mbSize = size / 1000f / 1000f;
    if (mbSize < 1000f) {
        return String.format("%d MB ", Math.round(mbSize));
    } else {//from  w  w w .  j  av a  2 s .co  m
        float gSize = mbSize / 1000f;
        if (gSize < 1000f) {
            return String.format("%d G ", Math.round(gSize));
        } else {
            float TSize = gSize / 1000f;
            return String.format("%d T ", Math.round(TSize));
        }

    }
}

From source file:Main.java

public static String distToString(Float dist) {
    if (dist == null) {
        return "";
    }//from  ww  w . ja  v  a  2  s.c om
    if (dist > 999) {
        return String.format("%.2fkm", dist / 1000);
    } else {
        return String.format("%dm", dist.intValue());
    }
}

From source file:Main.java

static void checkNotNull(Object o, String name) {
    if (o == null)
        throw new IllegalArgumentException(String.format("%s must be not null", new Object[] { name }));
}

From source file:Main.java

static void checkPositive(int number, String name) {
    if (number <= 0)
        throw new IllegalArgumentException(String.format("%s must not be null", name));
}

From source file:Main.java

public static void sleep(int sec) throws Exception {
    Log.i(TAG, String.format("Sleep %d second(s) forcibly.", sec));

    Thread.sleep(sec * 1000);/*  w ww .  ja  v  a 2  s  .c om*/
}