Example usage for java.text DecimalFormat setGroupingUsed

List of usage examples for java.text DecimalFormat setGroupingUsed

Introduction

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

Prototype

@Override
public void setGroupingUsed(boolean newValue) 

Source Link

Usage

From source file:fr.cls.atoll.motu.library.misc.netcdf.NetCdfReader.java

/**
 * Gets the standard z as string.//from w  w  w  . j ava  2  s .c o  m
 * 
 * @param value the value
 * @param roundingMode the rounding mode
 * @param desiredDecimalNumberDigits the desired decimal number of digits
 * 
 * @return the standard z as fmt string
 */
public static String getStandardZAsString(double value, RoundingMode roundingMode,
        int desiredDecimalNumberDigits) {

    int in = (int) (value);
    double frac = value - in;

    if (frac == 0d) {
        return NetCdfReader.getStandardZAsString(value);
    }

    DecimalFormat decimalFormat = new DecimalFormat();

    decimalFormat.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
    decimalFormat.setGroupingUsed(false);
    decimalFormat.setMinimumFractionDigits(desiredDecimalNumberDigits);
    decimalFormat.setMaximumFractionDigits(desiredDecimalNumberDigits);

    decimalFormat.setRoundingMode(roundingMode);

    return decimalFormat.format(value);
}

From source file:Matrix.java

/**
 * Print the matrix to the output stream. Line the elements up in columns
 * with a Fortran-like 'Fw.d' style format.
 * /*from w ww .  ja  v a  2s  .c  o  m*/
 * @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);
}

From source file:org.sakaiproject.tool.assessment.services.GradingService.java

/**
 * Validate a students numeric answer //  w w  w. j a  v a  2  s .c o m
 * @param The answer to validate
 * @return a Map containing either Real or Complex answer keyed by {@link #ANSWER_TYPE_REAL} or {@link #ANSWER_TYPE_COMPLEX} 
 */
public Map validate(String value) {
    HashMap map = new HashMap();
    if (value == null || value.trim().equals("")) {
        return map;
    }
    String trimmedValue = value.trim();
    boolean isComplex = true;
    boolean isRealNumber = true;

    BigDecimal studentAnswerReal = null;
    try {
        studentAnswerReal = new BigDecimal(trimmedValue);
    } catch (Exception e) {
        isRealNumber = false;
    }

    // Test for complex number only if it is not a BigDecimal
    Complex studentAnswerComplex = null;
    if (!isRealNumber) {
        try {
            DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
            df.setGroupingUsed(false);

            // Numerical format ###.## (decimal symbol is the point)
            ComplexFormat complexFormat = new ComplexFormat(df);
            studentAnswerComplex = complexFormat.parse(trimmedValue);

            // This is because there is a bug parsing complex number. 9i is parsed as 9
            if (studentAnswerComplex.getImaginary() == 0 && trimmedValue.contains("i")) {
                isComplex = false;
            }
        } catch (Exception e) {
            isComplex = false;
        }
    }

    Boolean isValid = isComplex || isRealNumber;
    if (!isValid) {
        throw new FinFormatException("Not a valid FIN Input. studentanswer=" + trimmedValue);
    }

    if (isRealNumber) {
        map.put(ANSWER_TYPE_REAL, studentAnswerReal);
    } else if (isComplex) {
        map.put(ANSWER_TYPE_COMPLEX, studentAnswerComplex);
    }

    return map;
}