Java Decimal Round roundedNumber(double inVal)

Here you can find the source of roundedNumber(double inVal)

Description

Format a number to a sensible precision

License

Open Source License

Parameter

Parameter Description
inVal value to format

Return

formatted String using local format

Declaration

public static String roundedNumber(double inVal) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.NumberFormat;

public class Main {
    /** Flexible number formatter with different decimal places */
    private static final NumberFormat FORMAT_FLEX = NumberFormat.getNumberInstance();

    /**/*from   ww w  .ja v a2 s . c  o  m*/
     * Format a number to a sensible precision
     * @param inVal value to format
     * @return formatted String using local format
     */
    public static String roundedNumber(double inVal) {
        // Set precision of formatter
        int numDigits = 0;
        if (inVal < 1.0)
            numDigits = 3;
        else if (inVal < 10.0)
            numDigits = 2;
        else if (inVal < 100.0)
            numDigits = 1;
        // set formatter
        FORMAT_FLEX.setMaximumFractionDigits(numDigits);
        FORMAT_FLEX.setMinimumFractionDigits(numDigits);
        return FORMAT_FLEX.format(inVal);
    }
}

Related

  1. roundDistanceFromMeterToKm(final double distanceInMeter)
  2. roundDouble(double iVal)
  3. roundDouble(double num, int decimal)
  4. roundDouble(double numero, int ceros_a_la_derecha)
  5. roundDouble(Double val, int precision)
  6. RoundFractionalTons(double d)
  7. roundNumDecimals(double d, int num)
  8. roundOffToTwoDecimal(double value)
  9. roundThreeDecimals(double d)