Example usage for java.text NumberFormat setMaximumFractionDigits

List of usage examples for java.text NumberFormat setMaximumFractionDigits

Introduction

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

Prototype

public void setMaximumFractionDigits(int newValue) 

Source Link

Document

Sets the maximum number of digits allowed in the fraction portion of a number.

Usage

From source file:com.example.awesomedogs.util.Stuff.java

/**
 * Converts some currency cents to a more readable format.
 * /*from ww w  .j  av  a 2 s.  c o m*/
 * @param cents Amount in cents, e.g. 123.
 * @return Formatted value, e.g. "1.23".
 */
public static String formatCents(int cents) {
    NumberFormat nf = NumberFormat.getNumberInstance();
    nf.setMinimumFractionDigits(2);
    nf.setMaximumFractionDigits(2);
    return nf.format(((double) cents / 100));
}

From source file:Utilities.java

public static String formatSize(long longSize, int decimalPos) {
    NumberFormat fmt = NumberFormat.getNumberInstance();
    if (decimalPos >= 0) {
        fmt.setMaximumFractionDigits(decimalPos);
    }//from   w  w w . ja va2  s .c o  m
    final double size = longSize;
    double val = size / (1024 * 1024);
    if (val > 1) {
        return fmt.format(val).concat(" MB");
    }
    val = size / 1024;
    if (val > 10) {
        return fmt.format(val).concat(" KB");
    }
    return fmt.format(val).concat(" bytes");
}

From source file:Main.java

public static String formatMoneyAmountOne(double amount) {
    Locale locale = new Locale("ru", "RU");
    NumberFormat format = NumberFormat.getCurrencyInstance(locale);
    format.setMinimumFractionDigits(0);//from ww w.  j av a  2  s  .c om
    format.setMaximumFractionDigits(1);
    String amountTxt = format.format(amount);
    String amountTxtValue = "";
    for (int i = 0; i < amountTxt.length(); i++) {
        if (Character.isDigit(amountTxt.charAt(i)) || amountTxt.charAt(i) == '.' || amountTxt.charAt(i) == ',')
            amountTxtValue = amountTxtValue + amountTxt.charAt(i);
    }

    amountTxt = amountTxtValue;

    if (amountTxt.endsWith(",00"))
        return (amountTxt.replace(",00", ""));
    else if (amountTxt.endsWith("0") && amountTxt.contains(",")) {
        return (amountTxt.substring(0, amountTxt.length() - 1));
    } else
        return (amountTxt);
}

From source file:Main.java

public static String formatNumber(double amount, int precision, Locale locale) {
    NumberFormat nf = NumberFormat.getNumberInstance(locale);
    nf.setMinimumFractionDigits(precision);
    nf.setMaximumFractionDigits(precision);
    return nf.format(amount);
}

From source file:com.doculibre.constellio.utils.FileSizeUtils.java

public static String formatSize(long fileSize, int decimalPos) {
    NumberFormat fmt = NumberFormat.getNumberInstance();
    if (decimalPos >= 0) {
        fmt.setMaximumFractionDigits(decimalPos);
    }// w w w . j a  v  a 2s  .c  o m
    final double size = fileSize;
    double val = size / (1024 * 1024);
    if (val > 1) {
        return fmt.format(val).concat(" MB");
    }
    val = size / 1024;
    if (val > 10) {
        return fmt.format(val).concat(" KB");
    }
    return fmt.format(val).concat(" bytes");
}

From source file:Main.java

public static String formatCurrency(double amount, int precision, Locale locale) {
    NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
    nf.setMinimumFractionDigits(precision);
    nf.setMaximumFractionDigits(precision);
    return nf.format(amount);
}

From source file:Main.java

public static String formatMoneyAmountItemised(double amount, Locale locale) {

    NumberFormat format = NumberFormat.getCurrencyInstance(locale);

    format.setMinimumFractionDigits(0);//from w w  w. j a  v a  2s.  co  m
    format.setMaximumFractionDigits(3);

    String amountTxt = format.format(amount);

    String amountTxtValue = "";
    for (int i = 0; i < amountTxt.length(); i++) {
        if (Character.isDigit(amountTxt.charAt(i)) || amountTxt.charAt(i) == '.' || amountTxt.charAt(i) == ',')
            amountTxtValue = amountTxtValue + amountTxt.charAt(i);
    }

    amountTxt = amountTxtValue;

    if (amountTxt.endsWith(",0"))
        return (amountTxt.replace(",0", ""));
    else if (amountTxt.endsWith(",00"))
        return (amountTxt.replace(",00", ""));
    else if (amountTxt.endsWith(",000"))
        return (amountTxt.replace(",000", ""));
    else
        return (amountTxt);

}

From source file:com.anrisoftware.globalpom.measurement.ValueToString.java

/**
 * Create number format based on the significant figures and decimal numbers
 * of the specified value./* ww w  . j a  v a  2  s .com*/
 * 
 * @param value
 *            the {@link Value}.
 * 
 * @return the {@link NumberFormat}.
 */
public static NumberFormat createFormat(Value value) {
    if (value.isExact()) {
        return new DecimalFormat(DECIMAL_PATTERN);
    }
    int sig = value.getSignificant();
    int dec = value.getDecimal();
    sig = FastMath.min(sig, 128);
    String pattern = START_ZERO + repeat(ZERO, sig) + EXP;
    NumberFormat format = new DecimalFormat(pattern);
    format.setMaximumFractionDigits(dec);
    return format;
}

From source file:com.norbl.util.StringUtil.java

public static String f1(double x) {
    NumberFormat f = NumberFormat.getInstance();
    f.setMaximumFractionDigits(1);
    f.setMinimumFractionDigits(1);/*  ww  w .j  a v  a2 s . c om*/
    return (f.format(x));
}

From source file:com.norbl.util.StringUtil.java

public static String f2(double x) {
    NumberFormat f = NumberFormat.getInstance();
    f.setMaximumFractionDigits(2);
    f.setMinimumFractionDigits(2);/*  w ww .  j a  va 2  s  .c  o m*/
    return (f.format(x));
}