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 toHex(byte[] bytes) {
    String temp = "";
    for (int index = 0; index < bytes.length; index++) {
        temp = temp + " " + String.format("%02x", bytes[index]);
    }/* w ww  .  jav a 2s  .c om*/
    return temp;
}

From source file:Main.java

public static String getSeperateAmount(double amount) {
    String double_format = "%1$.2f";
    String total = String.format(double_format, amount);
    // return String.format(format,
    // "" +/* ww  w.j  a v  a2  s .  co m*/
    return addSeparateSign((int) Float.parseFloat(total));
}

From source file:Main.java

public static <T> T notNull(final T object, final String message, final Object... values) {
    if (object == null) {
        throw new NullPointerException(String.format(message, values));
    }// w w  w.ja va  2 s  . c  o m
    return object;
}

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", new Object[] { name }));
}

From source file:Main.java

public static final String getFileSize(final long size) {
    if (size > 1073741824) {
        return String.format("%.2f", size / 1073741824.0) + " GB";
    } else if (size > 1048576) {
        return String.format("%.2f", size / 1048576.0) + " MB";
    } else if (size > 1024) {
        return String.format("%.2f", size / 1024.0) + " KB";
    } else {/*w ww. j  ava 2 s  .c  om*/
        return size + " B";
    }

}

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",
                new Object[] { Integer.valueOf(angle) }));
}

From source file:Main.java

/**
 * Process ppi value./*  ww  w  .ja va  2  s.c  o m*/
 * @param ppi Unit: pixels
 */
public static String toPPI(double ppi) {
    return String.format("%.0fppi", ppi);
}

From source file:Main.java

public static String stringFromDouble(double value) {
    if (value % 1 == 0) {
        return String.valueOf((int) value);
    } else {//from  ww  w  .  j ava  2s . c  o  m
        return String.format("%.2f", value);
    }
}

From source file:Main.java

public static String getTimeSpanFromSeconds(long secs) {
    if (TimeUnit.SECONDS.toHours(secs) == 0)
        return String.format("%dmin", TimeUnit.SECONDS.toMinutes(secs));
    else/*from  w w  w . ja v  a  2  s  .  c o  m*/
        return String.format("%dh %02dmin", TimeUnit.SECONDS.toHours(secs),
                TimeUnit.SECONDS.toMinutes(secs) - TimeUnit.HOURS.toMinutes(TimeUnit.SECONDS.toHours(secs)));
}

From source file:Main.java

public static String byte2HexString(byte[] bytes) {
    String ret = "";
    if (bytes != null) {
        for (Byte b : bytes) {
            ret += String.format("%02X", b.intValue() & 0xFF);
        }/*from  ww w  .  ja v  a2s .c  o  m*/
    }
    return ret;
}