Example usage for java.text DecimalFormat DecimalFormat

List of usage examples for java.text DecimalFormat DecimalFormat

Introduction

In this page you can find the example usage for java.text DecimalFormat DecimalFormat.

Prototype

public DecimalFormat(String pattern) 

Source Link

Document

Creates a DecimalFormat using the given pattern and the symbols for the default java.util.Locale.Category#FORMAT FORMAT locale.

Usage

From source file:Main.java

static void showPrice(BigDecimal price) {
    DecimalFormat format = new DecimalFormat("0.00");
    String text = String.format("Price: $%5s", format.format(price));
    System.out.println(text);//  w  w  w .  j  av  a 2s .c  o m
}

From source file:Main.java

public static double roundTwoDecimals(double d) {
    // try {//from   ww w  . ja  va2  s  .c om
    //DecimalFormat twoDForm = new DecimalFormat("#.#", DecimalFormatSymbols.getInstance(Locale.getDefault()));
    // NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
    DecimalFormat dFormat = new DecimalFormat("####,###,###.#");

    return Double.valueOf(dFormat.format(d));
    /**}
    catch (Exception exx)
    {
    return  d;
    }**/
}

From source file:Main.java

private static String FormetFileSize(long fileS) {
    DecimalFormat df = new DecimalFormat("#.00");
    String fileSizeString = "";
    String wrongSize = "0B";
    if (fileS == 0) {
        return wrongSize;
    }//ww  w .  ja  v a  2 s.c o  m
    if (fileS < 1024) {
        fileSizeString = df.format((double) fileS) + "B";
    } else if (fileS < 1048576) {
        fileSizeString = df.format((double) fileS / 1024) + "KB";
    } else if (fileS < 1073741824) {
        fileSizeString = df.format((double) fileS / 1048576) + "MB";
    } else {
        fileSizeString = df.format((double) fileS / 1073741824) + "GB";
    }
    return fileSizeString;
}

From source file:Main.java

public static String readableFileSize(long size) {
    if (size <= 0)
        return "0KB";
    final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
    int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
    return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}

From source file:Main.java

public static double getDPI(Activity activity) {
    return calculateDPI(getWidth(activity), getHeight(activity),
            Double.valueOf(new DecimalFormat("#.##").format(getScreenSize(activity))));
}

From source file:Main.java

public static float getTotalRAM() {
    RandomAccessFile reader = null;
    String load = null;//  w  w w  .ja  v  a2  s  .  c  o m
    DecimalFormat twoDecimalForm = new DecimalFormat("#.##");
    double totRam = 0;
    float lastValue = 0;
    try {
        reader = new RandomAccessFile("/proc/meminfo", "r");
        load = reader.readLine();

        // Get the Number value from the string
        Pattern p = Pattern.compile("(\\d+)");
        Matcher m = p.matcher(load);
        String value = "";
        while (m.find()) {
            value = m.group(1);
            // System.out.println("Ram : " + value);
        }
        reader.close();

        totRam = Double.parseDouble(value);
        // totRam = totRam / 1024;

        float mb = (float) (totRam / 1024.0f);
        float gb = (float) (totRam / 1048576.0f);
        float tb = (float) (totRam / 1073741824.0f);

        lastValue = gb;
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        // Streams.close(reader);
    }

    return lastValue;
}

From source file:Main.java

protected static final String encodingUTF8andSignPrefix(byte[] src, int srart, int length) throws Exception {
    byte[] b = new String(src, srart, length, "euc-kr").getBytes("utf-8");
    String str = new String(b).trim();
    str = str.substring(searchChar(str));
    DecimalFormat f = new DecimalFormat("###,###,###");
    f.setPositivePrefix("+");
    f.setNegativePrefix("-");
    return f.format(Double.parseDouble(str));
}

From source file:Main.java

/**
 * Convert a number of kilometers to miles.
 * @param km number of km to convert/*from   www .  j  a v a  2 s.  c o m*/
 * @return number of km in miles
 */
public static double asMiles(final double km) {
    DecimalFormat twoDForm = new DecimalFormat("#.##");
    return Double.valueOf(twoDForm.format(km * MILES_CONVERT));
}

From source file:Main.java

/**
 * // ww w .  java 2s. c om
 * @param number
 * @return
 */
public static String getDecimalNumber(long number, float divider) {
    DecimalFormat format = new DecimalFormat("#0.0");
    return format.format(number / divider);
}

From source file:Main.java

public static String BTC2Fiat(String btc) {
    double val = 0.0;

    try {//from  w ww  . j  av  a 2  s.c  o m
        val = Double.parseDouble(btc);
    } catch (NumberFormatException nfe) {
        val = 0.0;
    }

    DecimalFormat df = new DecimalFormat("######0.00");
    return df.format(BTC2Fiat(val));
}