Java Number Format Pattern roundNumber(Number number, DecimalFormat format)

Here you can find the source of roundNumber(Number number, DecimalFormat format)

Description

round Number

License

Apache License

Declaration

public static Number roundNumber(Number number, DecimalFormat format) 

Method Source Code

//package com.java2s;
/**// ww  w. j a va2 s  . c  om
 * Copyright 2013 BigML
 * Licensed under the Apache License, Version 2.0
 * http://www.apache.org/licenses/LICENSE-2.0
 */

import java.text.DecimalFormat;
import java.text.ParseException;

public class Main {
    public static Number roundNumber(Number number, DecimalFormat format) {
        /*
         * DecimalFormat.format() produces an IllegalArgumentException if the 
         * input cannot be formatted. We ignore ParseExceptions expecting that
         * any number which can be formatted can also be parsed by DecimalFormat.
         */
        Number formattedNumber = null;
        try {
            formattedNumber = format.parse(format.format(number));
        } catch (ParseException ex) {
        }
        return formattedNumber;
    }
}

Related

  1. normalizeNumberFormat(NumberFormat numberFormat, int scale, int precision)
  2. objectToString(Object obj, DecimalFormat fmt)
  3. print(float[] array, NumberFormat nf)
  4. resetDecimalFormat()
  5. resetDecimalFormatByLocale(Locale locale)
  6. stringFormat(BigDecimal value)
  7. stringToReais(String unformatted, boolean comSimbolo)
  8. strToFormatedNumber(String str)
  9. toAccountantFormat(String str, int scale)