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:com.crawler.app.run.CrawlSiteController.java

public static int getPageNumberEnd(String url) {
    try {/* w  ww. j  a v a 2s .com*/
        Connection.Response response =

                Jsoup.connect(url)
                        //enable for error urls
                        .ignoreHttpErrors(true)
                        //MAXIMUN TIME
                        .timeout(timeOut)
                        //This is to prevent producing garbage by attempting to parse a JPEG binary image
                        .ignoreContentType(true).execute();

        int status = response.statusCode();
        //after done
        if (status == 200) {
            org.jsoup.nodes.Document doc = response.parse();
            if (!pageNumberSelect.isEmpty()) {
                int pageNumber = -1;
                String strPageNumber = doc.select(pageNumberSelect).text();
                if (pageNumberIndexBegin > -1 && pageNumberIndexEnd > -1) {
                    String strSplit = "0";
                    for (int i = pageNumberIndexBegin; i <= pageNumberIndexEnd; i++) {
                        String ch = String.valueOf(strPageNumber.charAt(i));
                        if (tryParseIntByString(ch)) {
                            strSplit += ch;
                        }
                    }
                    pageNumber = Integer.parseInt(strSplit);
                } else {
                    pageNumber = Integer.parseInt(strPageNumber);
                }
                return pageNumber;

            } else {
                // get pagenumber with total product/numberproduct in page
                String strTotalProduct = "";
                if (totalProductPosition > -1) {
                    if (totalProductSelectPosition.isEmpty()) {
                        strTotalProduct = doc.select(totalProductSelect).get(totalProductPosition).text();
                    } else {
                        strTotalProduct = doc.select(totalProductSelect).get(totalProductPosition)
                                .select(totalProductSelectPosition).text();
                    }
                } else {
                    strTotalProduct = doc.select(totalProductSelect).text().toString().trim();
                }
                int totalProduct = -1;
                int i;
                // get totalproduct
                if (totalProductIndexBegin < 0) {
                    String strNumberTotalProduct = "0";
                    for (i = 0; i < strTotalProduct.length(); i++) {
                        String ch = String.valueOf(strTotalProduct.charAt(i));
                        if (ch.isEmpty() || ch.equals(" ")) {
                            break;
                        }
                        if (tryParseIntByString(ch)) {
                            strNumberTotalProduct += ch;
                        }
                    }
                    totalProduct = Integer.parseInt(strNumberTotalProduct);
                } else {
                    String strSplit = "0";
                    for (i = totalProductIndexBegin; i <= totalProductIndexEnd; i++) {
                        String ch = String.valueOf(strTotalProduct.charAt(i));
                        if (tryParseIntByString(ch)) {
                            strSplit += ch;
                        }
                    }
                    totalProduct = Integer.parseInt(strSplit);
                }
                // get number product in page
                int numberPage = -1;
                String strNumberProductInPage = "";
                if (numberProductInPagePosition > -1) {
                    if (numberProductInPageSelectPosition.isEmpty()) {
                        strNumberProductInPage = doc.select(numberProductInPageSelect)
                                .get(numberProductInPagePosition).text();
                    } else {
                        strNumberProductInPage = doc.select(numberProductInPageSelect)
                                .get(numberProductInPagePosition).select(numberProductInPageSelectPosition)
                                .text();
                    }
                } else {
                    strNumberProductInPage = doc.select(numberProductInPageSelect).text();
                }

                if (regexFromIndexEnd < 0 && regexToIndexEnd < 0) {
                    String strSplit = "0";
                    for (i = 0; i < strNumberProductInPage.length(); i++) {
                        String ch = String.valueOf(strNumberProductInPage.charAt(i));
                        if (ch.isEmpty() || ch.equals(" ")) {
                            break;
                        }
                        if (tryParseIntByString(ch)) {
                            strSplit += ch;
                        }
                    }
                    int numberProductInPage = Integer.parseInt(strSplit);
                    if (totalProduct > -1 && numberProductInPage > 0) {
                        if ((totalProduct % numberProductInPage) == 0) {
                            numberPage = totalProduct / numberProductInPage;
                        } else if (totalProduct > numberProductInPage) {
                            numberPage = totalProduct / numberProductInPage + 1;
                        }
                    }
                    return numberPage;
                } else {
                    //String[] arrStrNumberProductInPage = strNumberProductInPage.split(regex);
                    //String strNumberProductFrom = arrStrNumberProductInPage[0];
                    String strTotalProductReplace = String.valueOf(totalProduct);
                    if (!decimalFormatTotalProduct.isEmpty()) {
                        DecimalFormat formatter = new DecimalFormat(decimalFormatTotalProduct);
                        strTotalProductReplace = formatter.format(totalProduct);
                    }
                    strNumberProductInPage = strNumberProductInPage.replace(strTotalProductReplace, "");
                    int numberFrom = -1;
                    String strSplitFrom = "0";
                    for (i = regexFromIndexBegin; i <= regexFromIndexEnd; i++) {
                        String ch = String.valueOf(strNumberProductInPage.charAt(i));
                        if (tryParseIntByString(ch)) {
                            strSplitFrom += ch;
                        }
                    }
                    numberFrom = Integer.parseInt(strSplitFrom);

                    // String strNumberProductTo = arrStrNumberProductInPage[1];
                    int numberTo = -1;
                    String strSplitTo = "0";
                    for (i = regexToIndexBegin; i <= regexToIndexEnd; i++) {
                        String ch = String.valueOf(strNumberProductInPage.charAt(i));
                        if (tryParseIntByString(ch)) {
                            strSplitTo += ch;
                        }
                    }
                    numberTo = Integer.parseInt(strSplitTo);
                    int numberProductInPage = numberTo - numberFrom + 1;
                    if (totalProduct > -1 && numberProductInPage > 0) {
                        if ((totalProduct % numberProductInPage) == 0) {
                            numberPage = totalProduct / numberProductInPage;
                        } else if (totalProduct > numberProductInPage) {
                            numberPage = totalProduct / numberProductInPage + 1;
                        }
                    }
                    return numberPage;
                }
            }

        } else {
            return -1;
        }

    } catch (SocketTimeoutException se) {

        System.out.println("getContentOnly: SocketTimeoutException");
        System.out.println(se.getMessage());
        return -1;
    }

    catch (Exception e) {

        System.out.println("getContentOnly: Exception");
        e.printStackTrace();
        return -1;
    }

}

From source file:br.com.itfox.utils.Utils.java

public static String formatDecimal(float val) {
    String s = "";
    if (val > 0) {
        try {/* www.ja va2  s  .co  m*/
            DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(new Locale("en", "EN"));
            df.setMaximumFractionDigits(Preferences.MAXIMUM_FRACTION_DIGITS);
            df.setMinimumFractionDigits(Preferences.MINIMUM_FRACTION_DIGITS);
            df.setMinimumIntegerDigits(Preferences.MIMIMUM_INTEGER_DIGITS);
            df.setRoundingMode(Preferences.ROUNDING_MODE);
            s = String.valueOf(df.format(val));
        } catch (Exception ex) {
            br.com.itfox.utils.Logger.getLogger(ex, Utils.class.getName() + "***" + val + "***",
                    ex.getMessage());
        }
    }
    return s;

}

From source file:br.com.itfox.utils.Utils.java

public static String formatDecimal(double val) {
    String s = "";
    if (val > 0) {
        try {// ww  w .j a  v a  2 s  .  com
            DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(new Locale("en", "EN"));
            df.setMaximumFractionDigits(Preferences.MAXIMUM_FRACTION_DIGITS);
            df.setMinimumFractionDigits(Preferences.MINIMUM_FRACTION_DIGITS);
            df.setMinimumIntegerDigits(Preferences.MIMIMUM_INTEGER_DIGITS);
            df.setRoundingMode(Preferences.ROUNDING_MODE);
            s = String.valueOf(df.format(val));
        } catch (Exception ex) {
            br.com.itfox.utils.Logger.getLogger(ex, Utils.class.getName() + "***" + val + "***",
                    ex.getMessage());
        }
    }
    return s;

}

From source file:LineageSimulator.java

public static void writeVAFsToFile(String fileName, HashMap<Mutation.SNV, double[]> snvToVAFs,
        HashMap<Mutation.SNV, String> binaryProfiles, int numSamples) {
    String vafs = "";
    vafs += "#chrom\tpos\tdesc";
    if (binaryProfiles != null) {
        vafs += "\tprofile";
    }//from ww  w . jav  a  2s . co m
    vafs += "\tnormal";
    for (int i = 1; i < numSamples; i++) {
        vafs += "\tsample" + i;
    }
    vafs += "\n";
    DecimalFormat df = new DecimalFormat("#.####");
    for (Mutation.SNV snv : snvToVAFs.keySet()) {
        String v = (snv.chr + 1) + "\t" + snv.position + "\t" + snv.name;
        if (binaryProfiles != null) {
            v += "\t" + binaryProfiles.get(snv);
        }
        for (int i = 0; i < numSamples; i++) {
            v += "\t" + df.format(snvToVAFs.get(snv)[i]);
        }
        v += "\n";
        vafs += v;
    }
    writeOutputFile(fileName, vafs);
}

From source file:de.uniwue.info6.webapp.lists.ExGroupTableBean.java

/**
 *
 *
 * @param d/*from w ww  .ja  v  a2  s.c  o  m*/
 * @return
 */
public static String formatDouble(double d, int places) {
    DecimalFormat f = new DecimalFormat("#0." + StringUtils.repeat("0", places));
    return f.format(d);
}

From source file:com.moviejukebox.tools.StringTools.java

/**
 * Format the file size/*from w  w  w . j a  v  a 2s . c o  m*/
 *
 * @param fileSize
 * @return
 */
public static String formatFileSize(long fileSize) {

    String returnSize;
    if (fileSize < KB) {
        returnSize = fileSize + " Bytes";
    } else {
        String appendText;
        long divider;

        // resolve text to append and divider
        if (fileSize < MB) {
            appendText = " KB";
            divider = KB;
        } else if (fileSize < GB) {
            appendText = " MB";
            divider = MB;
        } else {
            appendText = " GB";
            divider = GB;
        }

        // resolve decimal format
        DecimalFormat df;
        long checker = (fileSize / divider);
        if (checker < 10) {
            df = FILESIZE_FORMAT_2;
        } else if (checker < 100) {
            df = FILESIZE_FORMAT_1;
        } else {
            df = FILESIZE_FORMAT_0;
        }

        // build string
        returnSize = df.format(((float) fileSize / (float) divider)) + appendText;
    }

    return returnSize;
}

From source file:com.prowidesoftware.swift.utils.SwiftFormatUtils.java

/**
 * Parses a Number into a SWIFT string number ####,## with truncated zero decimals and mandatory decimal separator.
 * <ul>//  w w w .j av a 2  s  . c  om
 *    <li>Example: 1234.00 -> 1234,</li>
 *    <li>Example: 1234 -> 1234,</li>
 *    <li>Example: 1234.56 -> 1234,56</li>
 * </ul>
 * @param number to parse
 * @return Number of the parsed amount or <code>null</code> if the number is null
 */
public static String getNumber(final Number number) {
    if (number != null) {
        final DecimalFormatSymbols symbols = new DecimalFormatSymbols();
        symbols.setDecimalSeparator(',');
        final DecimalFormat df = new DecimalFormat("0.##########", symbols);
        df.setParseBigDecimal(true);
        df.setDecimalSeparatorAlwaysShown(true);
        final String formatted = df.format(number);
        final String result = StringUtils.replaceChars(formatted, '.', ',');
        return result;
    }
    return null;
}

From source file:com.wms.utils.DataUtil.java

/**
 * @param number//from w w  w  .j a v a 2 s  .c om
 * @param pattern
 * @return
 * @author KhuongDV Ham format so thuc ve dang co max la 4 chu so thap phan.
 * Trim() so 0 vo nghia
 */
public static String getFormattedString4Digits(String number, String pattern) {
    double amount = 0;
    try {
        amount = Double.parseDouble(number);
        DecimalFormat formatter = new DecimalFormat(pattern);
        return formatter.format(amount);
    } catch (Exception ex) {
        return number;
    }
}

From source file:ml.shifu.shifu.core.PerformanceEvaluator.java

static void logResult(List<PerformanceObject> list, String info) {
    DecimalFormat df = new DecimalFormat("#.####");

    String formatString = "%10s %18s %10s %18s %15s %18s %10s %11s %10s";

    log.info("Start print: " + info);

    log.info(String.format(formatString, "ActionRate", "WeightedActionRate", "Recall", "WeightedRecall",
            "Precision", "WeightedPrecision", "FPR", "WeightedFPR", "BinLowestScore"));

    for (PerformanceObject po : list) {
        log.info(String.format(formatString, df.format(po.actionRate), df.format(po.weightedActionRate),
                df.format(po.recall), df.format(po.weightedRecall), df.format(po.precision),
                df.format(po.weightedPrecision), df.format(po.fpr), df.format(po.weightedFpr),
                po.binLowestScore));//from  w w  w  .  java  2s. c  o  m
    }

}

From source file:com.ieasy.basic.util.file.FileUtils.java

public static String formetFileSize(long fileS) {// ??
    DecimalFormat df = new DecimalFormat("#.00");
    String fileSizeString = "";
    if (fileS < 1024) {
        fileSizeString = df.format((double) fileS) + "B";
    } else if (fileS < 1048576) {
        fileSizeString = df.format((double) fileS / 1024) + "K";
    } else if (fileS < 1073741824) {
        fileSizeString = df.format((double) fileS / 1048576) + "M";
    } else {//ww  w.j  a  va 2s. c  o  m
        fileSizeString = df.format((double) fileS / 1073741824) + "G";
    }
    return fileSizeString;
}