Java Code Examples for java.text.DecimalFormat#setMinimumFractionDigits()

The following examples show how to use java.text.DecimalFormat#setMinimumFractionDigits() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Bg.java    From NightWatch with GNU General Public License v3.0 6 votes vote down vote up
public String unitized_string() {
    double value = sgv_double();
    DecimalFormat df = new DecimalFormat("#");
    if (value >= 400) {
        return "HIGH";
    } else if (value >= 40) {
        if(doMgdl()) {
            df.setMaximumFractionDigits(0);
            return df.format(value);
        } else {
            df.setMaximumFractionDigits(1);
            df.setMinimumFractionDigits(1);
            return df.format(unitized(value));
        }
    } else if (value >= 11) {
        return "LOW";
    } else {
        return "???";
    }
}
 
Example 2
Source File: NumberFormatProviderImpl.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adjusts the minimum and maximum fraction digits to values that
 * are reasonable for the currency's default fraction digits.
 */
private static void adjustForCurrencyDefaultFractionDigits(
        DecimalFormat format, DecimalFormatSymbols symbols) {
    Currency currency = symbols.getCurrency();
    if (currency == null) {
        try {
            currency = Currency.getInstance(symbols.getInternationalCurrencySymbol());
        } catch (IllegalArgumentException e) {
        }
    }
    if (currency != null) {
        int digits = currency.getDefaultFractionDigits();
        if (digits != -1) {
            int oldMinDigits = format.getMinimumFractionDigits();
            // Common patterns are "#.##", "#.00", "#".
            // Try to adjust all of them in a reasonable way.
            if (oldMinDigits == format.getMaximumFractionDigits()) {
                format.setMinimumFractionDigits(digits);
                format.setMaximumFractionDigits(digits);
            } else {
                format.setMinimumFractionDigits(Math.min(digits, oldMinDigits));
                format.setMaximumFractionDigits(digits);
            }
        }
    }
}
 
Example 3
Source File: NumberFormatProviderImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adjusts the minimum and maximum fraction digits to values that
 * are reasonable for the currency's default fraction digits.
 */
private static void adjustForCurrencyDefaultFractionDigits(
        DecimalFormat format, DecimalFormatSymbols symbols) {
    Currency currency = symbols.getCurrency();
    if (currency == null) {
        try {
            currency = Currency.getInstance(symbols.getInternationalCurrencySymbol());
        } catch (IllegalArgumentException e) {
        }
    }
    if (currency != null) {
        int digits = currency.getDefaultFractionDigits();
        if (digits != -1) {
            int oldMinDigits = format.getMinimumFractionDigits();
            // Common patterns are "#.##", "#.00", "#".
            // Try to adjust all of them in a reasonable way.
            if (oldMinDigits == format.getMaximumFractionDigits()) {
                format.setMinimumFractionDigits(digits);
                format.setMaximumFractionDigits(digits);
            } else {
                format.setMinimumFractionDigits(Math.min(digits, oldMinDigits));
                format.setMaximumFractionDigits(digits);
            }
        }
    }
}
 
Example 4
Source File: NumberFormatProviderImpl.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adjusts the minimum and maximum fraction digits to values that
 * are reasonable for the currency's default fraction digits.
 */
private static void adjustForCurrencyDefaultFractionDigits(
        DecimalFormat format, DecimalFormatSymbols symbols) {
    Currency currency = symbols.getCurrency();
    if (currency == null) {
        try {
            currency = Currency.getInstance(symbols.getInternationalCurrencySymbol());
        } catch (IllegalArgumentException e) {
        }
    }
    if (currency != null) {
        int digits = currency.getDefaultFractionDigits();
        if (digits != -1) {
            int oldMinDigits = format.getMinimumFractionDigits();
            // Common patterns are "#.##", "#.00", "#".
            // Try to adjust all of them in a reasonable way.
            if (oldMinDigits == format.getMaximumFractionDigits()) {
                format.setMinimumFractionDigits(digits);
                format.setMaximumFractionDigits(digits);
            } else {
                format.setMinimumFractionDigits(Math.min(digits, oldMinDigits));
                format.setMaximumFractionDigits(digits);
            }
        }
    }
}
 
Example 5
Source File: FormatHelper.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Format a grade from the root locale for display using the user's locale
 *
 * @param grade - string representation of a grade
 * @return
 */
public static String formatGradeForDisplay(final String grade) {
	if (StringUtils.isBlank(grade)) {
		return "";
	}

	String s;
	try {
		final BigDecimal d = convertStringToBigDecimal(grade, 2);

		final DecimalFormat dfFormat = (DecimalFormat) NumberFormat.getInstance(rl.getLocale());
		dfFormat.setMinimumFractionDigits(0);
		dfFormat.setMaximumFractionDigits(2);
		dfFormat.setGroupingUsed(true);
		s = dfFormat.format(d);
	} catch (final NumberFormatException e) {
		log.warn("Bad format, returning original string: {}", grade);
		s = grade;
	}

	return StringUtils.removeEnd(s, ".0");
}
 
Example 6
Source File: TestDecimalFormat.java    From util4j with Apache License 2.0 6 votes vote down vote up
public static void test1(DecimalFormat df) {
	//默认显示3位小数
	double d = 1.5555555;
	System.out.println(df.format(d));//1.556
	//设置小数点后最大位数为5
	df.setMaximumFractionDigits(5);
	df.setMinimumIntegerDigits(15);
	System.out.println(df.format(d));//1.55556
	df.setMaximumFractionDigits(2);
	System.out.println(df.format(d));//1.56
	//设置小数点后最小位数,不够的时候补0
	df.setMinimumFractionDigits(10);
	System.out.println(df.format(d));//1.5555555500
	//设置整数部分最小长度为3,不够的时候补0
	df.setMinimumIntegerDigits(3);
	System.out.println(df.format(d));
	//设置整数部分的最大值为2,当超过的时候会从个位数开始取相应的位数
	df.setMaximumIntegerDigits(2);
	System.out.println(df.format(d));
}
 
Example 7
Source File: Notifications.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static DecimalFormat getNumberFormatter(boolean doMgdl) {
    DecimalFormat df = new DecimalFormat("#");
    if (doMgdl) {
        df.setMaximumFractionDigits(0);
        df.setMinimumFractionDigits(0);
    } else {
        df.setMaximumFractionDigits(1);
        df.setMinimumFractionDigits(1);
    }

    return df;
}
 
Example 8
Source File: MagicSquareExampleFuncTest.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
public static String fixedWidthDoubletoString (final double x, final int w, final int d)
{
  final DecimalFormat fmt = (DecimalFormat) NumberFormat.getNumberInstance (CGlobal.DEFAULT_LOCALE);
  fmt.setMaximumFractionDigits (d);
  fmt.setMinimumFractionDigits (d);
  fmt.setGroupingUsed (false);
  final String s = fmt.format (x);
  return StringHelper.getWithLeading (s, w, ' ');
}
 
Example 9
Source File: BgToSpeech.java    From xDrip-Experimental with GNU General Public License v3.0 5 votes vote down vote up
private String calculateText(double value, SharedPreferences prefs) {
    boolean doMgdl = (prefs.getString("units", "mgdl").equals("mgdl"));

    String text = "";

    DecimalFormat df = new DecimalFormat("#");
    if (value >= 400) {
        text = "high";
    } else if (value >= 40) {
        if(doMgdl) {
            df.setMaximumFractionDigits(0);
            text =  df.format(value);
        } else {
            df.setMaximumFractionDigits(1);
            df.setMinimumFractionDigits(1);
            text =  df.format(value* Constants.MGDL_TO_MMOLL);
            if(tts.getLanguage().getLanguage().startsWith("en")){
                // in case the text has a comma in current locale but TTS defaults to English
                text.replace(",", ".");
            }
        }
    } else if (value > 12) {
        text =  "low";
    } else {
        text = "error";
    }
    Log.d("BgToSpeech", "text: " + text);
    return text;
}
 
Example 10
Source File: Matrix.java    From morpheus-core with Apache License 2.0 5 votes vote down vote up
/** Print the matrix to the output stream.   Line the elements up in
  * columns with a Fortran-like 'Fw.d' style format.
@param output Output stream.
@param w      Column width.
@param d      Number of digits after the decimal.
*/

public void print (PrintWriter output, int w, int d) {
   DecimalFormat format = new DecimalFormat();
   format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
   format.setMinimumIntegerDigits(1);
   format.setMaximumFractionDigits(d);
   format.setMinimumFractionDigits(d);
   format.setGroupingUsed(false);
   print(output,format,w+2);
}
 
Example 11
Source File: MonetaryUtil.java    From zap-android with MIT License 5 votes vote down vote up
private String formatAsFiatDisplayAmount(double value) {
    Locale loc = mContext.getResources().getConfiguration().locale;
    NumberFormat nf = NumberFormat.getNumberInstance(loc);
    DecimalFormat df = (DecimalFormat) nf;
    df.setMaximumFractionDigits(2);
    df.setMinimumFractionDigits(2);
    df.setMinimumIntegerDigits(1);
    df.setMaximumIntegerDigits(22);
    String result = df.format(value);
    return result;
}
 
Example 12
Source File: Matrix.java    From android-speaker-audioanalysis with MIT License 5 votes vote down vote up
/** Print the matrix to the output stream.   Line the elements up in
	 * columns with a Fortran-like 'Fw.d' style format.
@param output Output stream.
@param w      Column width.
@param d      Number of digits after the decimal.
	 */

	public void print (PrintWriter output, int w, int d) {
		DecimalFormat format = new DecimalFormat();
		format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
		format.setMinimumIntegerDigits(1);
		format.setMaximumFractionDigits(d);
		format.setMinimumFractionDigits(d);
		format.setGroupingUsed(false);
		print(output,format,w+2);
	}
 
Example 13
Source File: BgGraphBuilder.java    From xDrip-Experimental with GNU General Public License v3.0 5 votes vote down vote up
public String unitized_string(double value) {
    DecimalFormat df = new DecimalFormat("#");
    if (value >= 400) {
        return "HIGH";
    } else if (value >= 40) {
        if(doMgdl) {
            df.setMaximumFractionDigits(0);
            return df.format(value);
        } else {
            df.setMaximumFractionDigits(1);
            //next line ensures mmol/l value is XX.x always.  Required by PebbleSync, and probably not a bad idea.
            df.setMinimumFractionDigits(1);
            return df.format(mmolConvert(value));
        }
    } else if (value > 12) {
        return "LOW";
    } else {
        switch((int)value) {
            case 0:
                return "??0";
            case 1:
                return "?SN";
            case 2:
                return "??2";
            case 3:
                return "?NA";
            case 5:
                return "?NC";
            case 6:
                return "?CD";
            case 9:
                return "?AD";
            case 12:
                return "?RF";
            default:
                return "???";
        }
    }
}
 
Example 14
Source File: EditAlertActivity.java    From xDrip-Experimental with GNU General Public License v3.0 5 votes vote down vote up
public static DecimalFormat getNumberFormatter(boolean doMgdl) {
    DecimalFormat df = new DecimalFormat("#");
    if (doMgdl) {
        df.setMaximumFractionDigits(0);
        df.setMinimumFractionDigits(0);
    } else {
        df.setMaximumFractionDigits(1);
        df.setMinimumFractionDigits(1);
    }

    return df;
}
 
Example 15
Source File: StringUtil.java    From graql with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * @param value a value in the graph
 * @return the string representation of the value (using quotes if it is already a string)
 */
public static String valueToString(Object value) {
    if (value instanceof String) {
        return quoteString((String) value);
    } else if (value instanceof Double) {
        DecimalFormat df = new DecimalFormat("#", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
        df.setMinimumFractionDigits(1);
        df.setMaximumFractionDigits(12);
        df.setMinimumIntegerDigits(1);
        return df.format(value);
    } else {
        return value.toString();
    }
}
 
Example 16
Source File: CurrenciesUtil.java    From px-android with MIT License 5 votes vote down vote up
public static String getLocalizedAmountNoDecimals(final BigDecimal truncated, final Currency currency) {
    final DecimalFormatSymbols dfs = new DecimalFormatSymbols();
    dfs.setDecimalSeparator(currency.getDecimalSeparator());
    dfs.setGroupingSeparator(currency.getThousandsSeparator());
    final DecimalFormat df = new DecimalFormat();
    df.setDecimalFormatSymbols(dfs);
    df.setMinimumFractionDigits(0);
    df.setMaximumFractionDigits(0);
    return df.format(truncated);
}
 
Example 17
Source File: Bug8165466.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    DecimalFormat nf = (DecimalFormat) DecimalFormat
            .getPercentInstance(Locale.US);
    nf.setMaximumFractionDigits(3);
    nf.setMinimumFractionDigits(0);
    nf.setMultiplier(1);

    double d = 0.005678;
    String result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }

    d = 0.00;
    result = nf.format(d);
    if (!result.equals("0%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0%, Found: " + result
                + "]");
    }

    d = 0.005678;
    result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }

    //checking with the non zero value
    d = 0.005678;
    result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }

    d = 9.00;
    result = nf.format(d);
    if (!result.equals("9%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 9%, Found: " + result
                + "]");
    }

    d = 0.005678;
    result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }
}
 
Example 18
Source File: Wilcoxon.java    From KEEL with GNU General Public License v3.0 4 votes vote down vote up
/**
 * In this method, all possible pairwise Wilcoxon comparisons are performed
 *
 * @param newData Array with the results of the method
 * @param newAlgorithms A vector of String with the names of the algorithms
 */
public static void doWilcoxon(double newData[][], String newAlgorithms[]) {

    outputFileName = Configuration.getPath();

    String outputString = "";
    outputString = header();

    data = new double[newData[0].length][newData.length];
    algorithms = new String[newAlgorithms.length];
    columns = data[0].length;
    rows = data.length;

    //reverse data matrix
    for (int i = 0; i < data.length; i++) {
        for (int j = 0; j < data[0].length; j++) {
            data[i][j] = newData[j][i];
        }
    }
    System.arraycopy(newAlgorithms, 0, algorithms, 0, newAlgorithms.length);

    wilcoxonRanks = new double[columns][columns];
    exactPValues = new double[columns][columns];
    asymptoticPValues = new double[columns][columns];
    confidenceIntervals95 = new String[columns][columns];
    confidenceIntervals90 = new String[columns][columns];
    exactConfidence90 = new double[columns][columns];
    exactConfidence95 = new double[columns][columns];

    wins90 = new int[columns];
    wins95 = new int[columns];

    draw90 = new int[columns];
    draw95 = new int[columns];

    Arrays.fill(wins90,0);
    Arrays.fill(wins95,0);

    Arrays.fill(draw90,0);
    Arrays.fill(draw95,0);

    nf = (DecimalFormat) DecimalFormat.getInstance();
    nf.setMaximumFractionDigits(6);
    nf.setMinimumFractionDigits(0);

    DecimalFormatSymbols dfs = nf.getDecimalFormatSymbols();
    dfs.setDecimalSeparator('.');
    nf.setDecimalFormatSymbols(dfs);

    Files.writeFile(outputFileName, outputString);

    computeBody();

    outputString = footer();

    Files.addToFile(outputFileName, outputString);

    //write summary file
    outputSummaryFileName = outputFileName.substring(0, outputFileName.length() - 4) + "_Summary.tex";

    outputString = headerSummary();

    Files.addToFile(outputSummaryFileName, outputString);

    computeSummary();

    outputString = footer();

    Files.addToFile(outputSummaryFileName, outputString);

}
 
Example 19
Source File: Bug8165466.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    DecimalFormat nf = (DecimalFormat) DecimalFormat
            .getPercentInstance(Locale.US);
    nf.setMaximumFractionDigits(3);
    nf.setMinimumFractionDigits(0);
    nf.setMultiplier(1);

    double d = 0.005678;
    String result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }

    d = 0.00;
    result = nf.format(d);
    if (!result.equals("0%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0%, Found: " + result
                + "]");
    }

    d = 0.005678;
    result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }

    //checking with the non zero value
    d = 0.005678;
    result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }

    d = 9.00;
    result = nf.format(d);
    if (!result.equals("9%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 9%, Found: " + result
                + "]");
    }

    d = 0.005678;
    result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }
}
 
Example 20
Source File: ValueRounder.java    From development with Apache License 2.0 3 votes vote down vote up
/**
 * Rounds a value by using the specified scale with half up rounding and
 * without grouping. Even if the last part of the decimal value is 0 in
 * specified fraction digits, always all decimal places are filled with 0.
 * (e.g. value 1.2, 5 fraction digits, en locale => '1.20000' will be
 * returned)
 * 
 * @param value
 *            Value to be formatted.
 * @param locale
 *            Current user's locale for formatting.
 * @param fractionDigits
 *            Number of decimal places.
 * @return
 */
public static String roundValue(BigDecimal value, Locale locale,
        int fractionDigits) {
    DecimalFormat df = new DecimalFormat();
    df.setDecimalFormatSymbols(new DecimalFormatSymbols(locale));
    df.setGroupingUsed(false);
    df.setMaximumFractionDigits(fractionDigits);
    df.setMinimumFractionDigits(fractionDigits);
    df.setRoundingMode(RoundingMode.HALF_UP);
    return df.format(value);
}