Example usage for java.text NumberFormat setRoundingMode

List of usage examples for java.text NumberFormat setRoundingMode

Introduction

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

Prototype

public void setRoundingMode(RoundingMode roundingMode) 

Source Link

Document

Sets the java.math.RoundingMode used in this NumberFormat.

Usage

From source file:com.scf.core.EnvTest.java

public static String formatMoney(BigDecimal money, int len) {
    NumberFormat formater = null;
    if (len == 0) {
        formater = new DecimalFormat("###,###");
    } else {//from  w w  w.  j a v  a2s .c  om
        StringBuffer buff = new StringBuffer();
        buff.append("###,##0.");
        for (int i = 0; i < len; i++) {
            buff.append("0");
        }
        formater = new DecimalFormat(buff.toString());
        formater.setRoundingMode(RoundingMode.HALF_UP);
    }
    return formater.format(money);
}

From source file:at.alladin.rmbt.qos.testscript.TestScriptInterpreter.java

/**
 * //from   w  w w.  j  a  v a 2  s.c  o m
 * @param args
 * @return
 * @throws ScriptException
 */
private static Object parse(String[] args, Hstore hstore, Object object, ResultOptions options)
        throws ScriptException {

    if (object == null) {
        throw new ScriptException(ScriptException.ERROR_RESULT_IS_NULL + " PARSE");
    }

    HstoreParser<?> parser = hstore.getParser(object.getClass());

    if (args.length < 1) {
        throw new ScriptException(ScriptException.ERROR_INVALID_ARGUMENT_COUNT + " PARSE: " + args.length);
    }
    if (parser == null) {
        throw new ScriptException(ScriptException.ERROR_PARSER_IS_NULL + " PARSE");
    }

    try {
        Pattern p = PATTERN_ARRAY;
        Matcher m = p.matcher(args[0]);

        if (m.find()) {
            String param = m.group(1);
            int index = Integer.valueOf(m.group(2));
            Object array = parser.getValue(param, object);

            Object indexedObject = null;
            if (array != null) {
                if (List.class.isAssignableFrom(array.getClass())) {
                    indexedObject = ((List<?>) array).get(index);
                } else if (Collection.class.isAssignableFrom(array.getClass())) {
                    Iterator<?> iterator = ((Collection<?>) array).iterator();
                    int counter = 0;
                    while (iterator.hasNext()) {
                        Object o = iterator.next();
                        if ((counter++) == index) {
                            indexedObject = o;
                            break;
                        }
                    }
                }

                if (args.length > 1) {
                    String[] nextArgs = new String[args.length - 1];
                    nextArgs = Arrays.copyOfRange(args, 1, args.length);
                    return parse(nextArgs, hstore, indexedObject, options);
                } else {
                    return indexedObject;
                }
            }
        } else {
            Object value = parser.getValue(args[0], object);
            if (args.length > 1) {
                try {
                    long divisor = Long.parseLong(args[1]);
                    int precision = 2;
                    boolean groupingUsed = false;
                    if (args.length > 2) {
                        precision = Integer.parseInt(args[2]);
                    }
                    if (args.length > 3) {
                        groupingUsed = ("t".equals(args[3].toLowerCase())
                                || "true".equals(args[3].toLowerCase()));
                    }
                    NumberFormat format = (options != null ? DecimalFormat.getInstance(options.getLocale())
                            : DecimalFormat.getInstance());
                    format.setMaximumFractionDigits(precision);
                    format.setGroupingUsed(groupingUsed);
                    format.setRoundingMode(RoundingMode.HALF_UP);
                    //System.out.println("Converting number: " + args[0] + "=" + String.valueOf(value));
                    BigDecimal number = new BigDecimal(String.valueOf(value));
                    return format.format(number.divide(new BigDecimal(divisor)));
                } catch (Exception e) {
                    //can not return parsed element
                }
            }
            //System.out.println("PARAM object: " + args[0] + " -> " + value + " of " + object.toString());
            return value;
        }
    } catch (HstoreParseException e) {
        throw new ScriptException(ScriptException.ERROR_UNKNOWN + " PARSE: " + e.getMessage());
    } catch (Throwable t) {
        throw new ScriptException(ScriptException.ERROR_UNKNOWN + " PARSE: " + t.getMessage());
    }

    return null;
}

From source file:com.ibk.ltw.domain.Product.java

public String getVatAsText() {
    BigDecimal vatInPercent = new BigDecimal(getVatInPerThousand()).movePointLeft(3);
    NumberFormat percentFormat = NumberFormat.getPercentInstance(Locale.US);
    percentFormat.setMaximumFractionDigits(1);
    percentFormat.setRoundingMode(RoundingMode.HALF_DOWN);
    return percentFormat.format(vatInPercent);
}

From source file:Main.java

private JFormattedTextField setFormat(JFormattedTextField jft, Locale local1, int minFra, int maxFra) {
    NumberFormat numberFormat;
    Locale local = local1;/*from  w  w  w  .  j  a v  a2 s  . co m*/
    int setMin = minFra;
    int setMax = maxFra;
    numberFormat = NumberFormat.getCurrencyInstance(local);
    numberFormat.setMinimumFractionDigits(setMin);
    numberFormat.setMaximumFractionDigits(setMax);
    numberFormat.setRoundingMode(RoundingMode.HALF_UP);
    jft = new JFormattedTextField(numberFormat);
    jft.setValue(new Double(342.796));
    return jft;
}

From source file:com.zte.ums.zenap.itm.agent.plugin.linux.util.Calculator.java

public String calculateItem(Map variables, String formula) throws MonitorException {

    Set vKeySet = variables.keySet();
    Object[] keys = vKeySet.toArray();
    Arrays.sort(keys);/*from  w  w w . j  av  a 2 s  .  c o  m*/
    JexlContext jexlContext = new MapContext();
    for (int i = keys.length - 1; i >= 0; i--) {
        String s = keys[i].toString();
        String s1 = s.replace('.', 'a');
        formula = formula.replaceAll(s, s1);
        jexlContext.set(s1, variables.get(s));
    }

    try {
        while (true) {
            int eIndex = formula.indexOf("E");
            if (eIndex == -1) {
                break;
            }
            int startIndex = getBackwardNumIndex(formula, eIndex);
            int endIndex = getForwardNumIndex(formula, eIndex);
            String eNum = formula.substring(eIndex + 1, endIndex);
            String tenNumString = null;
            int num = Integer.parseInt(eNum);
            if (num > 5) {
                int tenNum = 1;
                for (int i = 0; i < (num - 5); i++) {
                    tenNum = tenNum * 10;
                }
                tenNumString = "100000.0 * " + Integer.toString(tenNum);
            } else if (num > 0) {
                int tenNum = 1;
                for (int i = 0; i < num; i++) {
                    tenNum = tenNum * 10;
                }
                tenNumString = Integer.toString(tenNum);
            } else if (num < 0) {
                double tenNum = 1;
                for (int i = 0; i > num; i--) {
                    tenNum = tenNum * 0.1;
                }
                tenNumString = Double.toString(tenNum);
            }
            String variable = formula.substring(startIndex, endIndex);
            String headVariable = formula.substring(startIndex, eIndex);
            formula = formula.replaceFirst(variable, "(" + headVariable + " * " + tenNumString + ")");
        }
        JexlEngine jexlEngine = new JexlEngine();
        Expression expression = jexlEngine.createExpression(formula);
        Object result = expression.evaluate(jexlContext);
        if (result instanceof Double) {
            Double dd = (Double) result;

            NumberFormat formater = new DecimalFormat("#.00");
            formater.setRoundingMode(RoundingMode.HALF_UP);
            return formater.format(dd);
        }
        return result.toString();
    } catch (Exception e) {
        throw new MonitorException("Error!Something wrong happened! ", e);
    }
}

From source file:org.hoteia.qalingo.core.domain.CurrencyReferential.java

public NumberFormat getEcoCurrencyformat() {
    NumberFormat formatter = getStandardCurrencyformat();
    formatter.setGroupingUsed(true);//from w  w  w.jav a  2 s.c  o  m
    formatter.setParseIntegerOnly(false);
    formatter.setRoundingMode(RoundingMode.HALF_EVEN);

    formatter.setMaximumFractionDigits(2);
    formatter.setMinimumFractionDigits(2);

    formatter.setMaximumIntegerDigits(1000000);
    formatter.setMinimumIntegerDigits(1);
    return formatter;
}

From source file:com.mgmtp.perfload.perfalyzer.PerfAlyzerModule.java

@Provides
@FloatFormat//from  w ww .  ja v  a2 s  .c  om
NumberFormat provideFloatFormat(final Locale locale) {
    DecimalFormatSymbols dfs = new DecimalFormatSymbols(locale);
    NumberFormat nf = new DecimalFormat("0.00", dfs);
    nf.setGroupingUsed(false);
    nf.setRoundingMode(RoundingMode.HALF_UP);
    return nf;
}

From source file:com.mgmtp.perfload.perfalyzer.PerfAlyzerModule.java

@Provides
@IntFormat//from ww  w .j a va  2s .c om
NumberFormat provideIntFormat(final Locale locale) {
    NumberFormat nf = NumberFormat.getIntegerInstance(locale);
    nf.setGroupingUsed(false);
    nf.setRoundingMode(RoundingMode.HALF_UP);
    return nf;
}

From source file:com.vgi.mafscaling.Rescale.java

private void createControlPanel(JPanel dataPanel) {
    JPanel cntlPanel = new JPanel();
    GridBagConstraints gbl_ctrlPanel = new GridBagConstraints();
    gbl_ctrlPanel.insets = insets3;/*from   w ww  .  ja v  a2 s . c  o  m*/
    gbl_ctrlPanel.anchor = GridBagConstraints.PAGE_START;
    gbl_ctrlPanel.fill = GridBagConstraints.HORIZONTAL;
    gbl_ctrlPanel.weightx = 1.0;
    gbl_ctrlPanel.gridx = 0;
    gbl_ctrlPanel.gridy = 0;
    dataPanel.add(cntlPanel, gbl_ctrlPanel);

    GridBagLayout gbl_cntlPanel = new GridBagLayout();
    gbl_cntlPanel.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 };
    gbl_cntlPanel.rowHeights = new int[] { 0, 0 };
    gbl_cntlPanel.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 };
    gbl_cntlPanel.rowWeights = new double[] { 0 };
    cntlPanel.setLayout(gbl_cntlPanel);

    NumberFormat doubleFmt = NumberFormat.getNumberInstance();
    doubleFmt.setGroupingUsed(false);
    doubleFmt.setMaximumIntegerDigits(1);
    doubleFmt.setMinimumIntegerDigits(1);
    doubleFmt.setMaximumFractionDigits(3);
    doubleFmt.setMinimumFractionDigits(1);
    doubleFmt.setRoundingMode(RoundingMode.HALF_UP);

    NumberFormat scaleDoubleFmt = NumberFormat.getNumberInstance();
    scaleDoubleFmt.setGroupingUsed(false);
    scaleDoubleFmt.setMaximumIntegerDigits(1);
    scaleDoubleFmt.setMinimumIntegerDigits(1);
    scaleDoubleFmt.setMaximumFractionDigits(8);
    scaleDoubleFmt.setMinimumFractionDigits(1);
    scaleDoubleFmt.setRoundingMode(RoundingMode.HALF_UP);

    GridBagConstraints gbc_cntlPanelLabel = new GridBagConstraints();
    gbc_cntlPanelLabel.anchor = GridBagConstraints.EAST;
    gbc_cntlPanelLabel.insets = new Insets(2, 3, 2, 1);
    gbc_cntlPanelLabel.gridx = 0;
    gbc_cntlPanelLabel.gridy = 0;

    GridBagConstraints gbc_cntlPanelInput = new GridBagConstraints();
    gbc_cntlPanelInput.anchor = GridBagConstraints.WEST;
    gbc_cntlPanelInput.insets = new Insets(2, 1, 2, 3);
    gbc_cntlPanelInput.gridx = 1;
    gbc_cntlPanelInput.gridy = 0;

    cntlPanel.add(new JLabel("New Max V"), gbc_cntlPanelLabel);

    newMaxVFmtTextBox = new JFormattedTextField(doubleFmt);
    newMaxVFmtTextBox.setColumns(7);
    newMaxVFmtTextBox.addPropertyChangeListener("value", new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent e) {
            Object source = e.getSource();
            if (source == newMaxVFmtTextBox)
                updateNewMafScale();
        }
    });
    cntlPanel.add(newMaxVFmtTextBox, gbc_cntlPanelInput);

    gbc_cntlPanelLabel.gridx += 2;
    cntlPanel.add(new JLabel("Min V"), gbc_cntlPanelLabel);

    minVFmtTextBox = new JFormattedTextField(doubleFmt);
    minVFmtTextBox.setColumns(7);
    minVFmtTextBox.addPropertyChangeListener("value", new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent e) {
            Object source = e.getSource();
            if (source == minVFmtTextBox)
                updateNewMafScale();
        }
    });
    gbc_cntlPanelInput.gridx += 2;
    cntlPanel.add(minVFmtTextBox, gbc_cntlPanelInput);

    gbc_cntlPanelLabel.gridx += 2;
    cntlPanel.add(new JLabel("Max Unchanged"), gbc_cntlPanelLabel);

    maxVUnchangedFmtTextBox = new JFormattedTextField(doubleFmt);
    maxVUnchangedFmtTextBox.setColumns(7);
    maxVUnchangedFmtTextBox.addPropertyChangeListener("value", new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent e) {
            Object source = e.getSource();
            if (source == maxVUnchangedFmtTextBox)
                updateNewMafScale();
        }
    });
    gbc_cntlPanelInput.gridx += 2;
    cntlPanel.add(maxVUnchangedFmtTextBox, gbc_cntlPanelInput);

    gbc_cntlPanelLabel.gridx += 2;
    cntlPanel.add(new JLabel("Mode deltaV"), gbc_cntlPanelLabel);

    modeDeltaVFmtTextBox = new JFormattedTextField(scaleDoubleFmt);
    modeDeltaVFmtTextBox.setColumns(7);
    modeDeltaVFmtTextBox.setEditable(false);
    modeDeltaVFmtTextBox.setBackground(new Color(210, 210, 210));
    gbc_cntlPanelInput.gridx += 2;
    cntlPanel.add(modeDeltaVFmtTextBox, gbc_cntlPanelInput);
}

From source file:fr.hoteia.qalingo.core.web.util.impl.RequestUtilImpl.java

/**
 *
 *//*from  ww  w.  j a  v  a2 s .  c o m*/
public NumberFormat getNumberFormat(final RequestData requestData, final String currencyCode,
        final RoundingMode roundingMode, final int maximumFractionDigits, final int minimumFractionDigits,
        final int maximumIntegerDigits, final int minimumIntegerDigits) throws Exception {
    final Localization localization = requestData.getLocalization();
    final Locale locale = localization.getLocale();
    NumberFormat formatter = NumberFormat.getCurrencyInstance(locale);
    formatter.setGroupingUsed(true);
    formatter.setParseIntegerOnly(false);
    formatter.setRoundingMode(roundingMode);
    Currency currency = Currency.getInstance(currencyCode);
    formatter.setCurrency(currency);

    formatter.setMaximumFractionDigits(maximumFractionDigits);
    formatter.setMinimumFractionDigits(minimumFractionDigits);

    formatter.setMaximumIntegerDigits(maximumIntegerDigits);
    formatter.setMinimumIntegerDigits(minimumIntegerDigits);

    return formatter;
}