Example usage for java.text DecimalFormat format

List of usage examples for java.text DecimalFormat format

Introduction

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

Prototype

public final String format(double number) 

Source Link

Document

Specialization of format.

Usage

From source file:net.nosleep.superanalyzer.util.Misc.java

public static String getFormattedByteCount(double bytes) {
    double result = bytes;
    String units = "MB";

    // make it into KB
    result /= 1024.0;//w w  w. j  a va  2  s.c om

    // make it into MB
    result /= 1024.0;

    // if more than a gigabyte, make it into GB
    if (result / 1024.0 > 1.0) {
        result /= 1024.0;
        units = "GB";

        // if more than a terabyte, make it into TB
        if (result / 1024.0 > 1.0) {
            result /= 1024.0;
            units = "TB";
        }
    }

    DecimalFormat decimalFormat = new DecimalFormat("0.0");
    return decimalFormat.format(result) + " " + units;
}

From source file:hr.fer.tel.rovkp.homework03.task01.JokesCollection.java

/**
 * Outputs similarity matrix to CSV file
 * in format ID1, ID2, similarity/*from   w  w  w  .  j a  va2  s  .c  o  m*/
 * @param similarityMatrix matrix to print
 * @param file CSV file to create
 * @throws FileNotFoundException 
 */
public static void similarityMatrixAsCsv(float[][] similarityMatrix, String file) throws FileNotFoundException {
    try (PrintWriter out = new PrintWriter(file)) {
        for (int i = 0; i < similarityMatrix.length; i++) {
            for (int j = 0; j <= i; j++) {
                if (similarityMatrix[i][j] != 0) {
                    DecimalFormat format = new DecimalFormat("###.####");
                    out.println((j + 1) + ", " + (i + 1) + ", " + format.format(similarityMatrix[i][j]));
                }
            }
        }
    }
}

From source file:com.alibaba.otter.manager.web.common.NumberFormatUtil.java

public static String formatDelay(Number data) {
    if (data == null) {
        return StringUtils.EMPTY;
    }/* ww  w  .java  2s  .c  om*/
    long t = data.longValue();
    if (t < 0) {
        return String.valueOf(t);
    }
    int hour = 0;
    int minute = 0;

    while (t >= 60 * 60 * 1000) {
        hour++;
        t -= 60 * 60 * 1000;
    }

    while (t >= 60 * 1000) {
        minute++;
        t -= 60 * 1000;
    }

    List<String> result = new ArrayList<String>();
    if (hour > 0) {
        result.add(hour + " h");
    }
    if (minute > 0) {
        result.add(minute + " m");
    }

    if (t > 0) {
        DecimalFormat format = new DecimalFormat(PATTERN);
        result.add(format.format((t * 1.0) / 1000) + " s");
    }

    if (result.size() == 0) {
        return "0";
    }
    return StringUtils.join(result, " ");
}

From source file:io.hops.hopsworks.common.util.FormatUtils.java

public static String timeInSec(Long t) {
    DecimalFormat format = new DecimalFormat("#.#");
    if (t == null) {
        return "";
    }//w w w  .ja v a2s  . c  o m
    Double time = (double) t;
    if (time < m) {
        return format.format(time) + "s";
    }
    if (time < h) {
        return format.format(time / m) + "m";
    }
    if (time < d) {
        return format.format(time / h) + "h";
    }
    return format.format(time / d) + "d";
}

From source file:ch.bender.evacuate.Helper.java

/**
 * Appends a number suffix to the name of the Path object. If the flag BeforeExtension is
 * <code>true</code>, the number is appended before the last dot in the file name.
 * <p>/* w  w w .  j a va 2s  .  co  m*/
 * See examples in {@link #appendNumberSuffix(Path, int)}
 * <p>
 * 
 * @param aPath
 *        the path whose name should be suffixed
 * @param aNumber
 *        the number to suffix
 * @param aBeforeExtension 
 *        <code>true</code>: the extension stays the last part of the filename.
 * @return the new path
 */
public static Path appendNumberSuffix(Path aPath, int aNumber, boolean aBeforeExtension) {
    DecimalFormat df = new DecimalFormat("00");
    String suffix = "_" + df.format(aNumber);

    String parent = aPath.getParent() == null ? "" : aPath.getParent().toString();
    String extension = "";
    String name = aPath.getFileName().toString();

    if (aBeforeExtension) {
        extension = FilenameUtils.getExtension(name);

        if (extension.length() > 0) {
            extension = "." + extension;
        }
        name = FilenameUtils.getBaseName(name);
    }

    return Paths.get(parent, name + suffix + extension);
}

From source file:Util.java

public static String[] getMemoryInfo() {
    double max = Runtime.getRuntime().maxMemory() / 1024; // maxMemory is the upper limit the jvm can use
    double allocated = Runtime.getRuntime().totalMemory() / 1024; // totalMemory the size of the current allocation
    // pool//from w w w  . j  a  v a  2 s.  c o  m
    double nonAllocated = max - allocated; // non allocated memory till jvm limit
    double cached = Runtime.getRuntime().freeMemory() / 1024; // freeMemory the unused memory in the allocation pool
    double used = allocated - cached; // really used memory
    double useable = max - used; // allocated, but non-used and non-allocated memory
    DecimalFormat df = new DecimalFormat(" (0.0000'%')");
    DecimalFormat df2 = new DecimalFormat(" # 'KB'");
    return new String[] { //
            "+----", //
            "| Global Memory Informations at " + getRealTime().toString() + ":", //
            "|    |", //
            "| Allowed Memory:" + df2.format(max), //
            "|    |= Allocated Memory:" + df2.format(allocated) + df.format(allocated / max * 100), //
            "|    |= Non-Allocated Memory:" + df2.format(nonAllocated) + df.format(nonAllocated / max * 100), //
            "| Allocated Memory:" + df2.format(allocated), //
            "|    |= Used Memory:" + df2.format(used) + df.format(used / max * 100), //
            "|    |= Unused (cached) Memory:" + df2.format(cached) + df.format(cached / max * 100), //
            "| Useable Memory:" + df2.format(useable) + df.format(useable / max * 100), //
            "+----" //
    };
}

From source file:de.uniwue.info6.webapp.admin.SubmissionRow.java

/**
 *
 *
 * @param d//from   ww  w . j  a va2 s.c  om
 * @return
 */
private static String formatDouble(double d, int places) {
    DecimalFormat f = new DecimalFormat("#0." + StringUtils.repeat("0", places));
    return f.format(d);
}

From source file:com.ms.app.web.commons.tools.CurrencyFormattor.java

/**
 * "###.####"-->"#.00" floatPrice-->formatPrice(?)
 *//* w  ww.ja  va 2s. com*/
public static String float2formatPrice(Float price) {
    if (price == null) {
        return null;
    }

    BigDecimal b = new BigDecimal(price);
    float f = b.setScale(SCALE, BigDecimal.ROUND_HALF_UP).floatValue();
    DecimalFormat fnum = new DecimalFormat(CURRENCY);

    return fnum.format(f);
}

From source file:Main.java

@NonNull
static String convertAmount(int amount) {
    DecimalFormat formatter = (DecimalFormat) NumberFormat.getCurrencyInstance();
    formatter.setCurrency(Currency.getInstance("EUR"));
    String symbol = formatter.getCurrency().getSymbol();
    formatter.setNegativePrefix(symbol + "-");
    formatter.setNegativeSuffix("");

    return formatter.format((double) amount / 100.0);
}

From source file:net.ceos.project.poi.annotated.bean.ObjectMaskBuilder.java

/**
 * Transform BigDecimal values to validate.
 * /*  ww  w .j  ava 2 s  . co  m*/
 * @param fieldName
 * @param value
 * @return
 */
private static BigDecimal transformValuesToValidate(String fieldName, BigDecimal value) {
    ObjectMask a = new ObjectMask();
    try {
        Field f = a.getClass().getDeclaredField(fieldName);

        if (f.isAnnotationPresent(XlsElement.class)) {
            XlsElement xlsAnnotation = (XlsElement) f.getAnnotation(XlsElement.class);

            if (StringUtils.isNotBlank(xlsAnnotation.transformMask())) {
                DecimalFormat df = new DecimalFormat(xlsAnnotation.transformMask());
                String formattedValue = df.format((BigDecimal) value);
                value = BigDecimal.valueOf(Double.valueOf(formattedValue.replace(",", ".")));
            }
        }

    } catch (NoSuchFieldException | SecurityException e) {
        e.printStackTrace();
    }
    return value;
}