Java Decimal Round roundDecimals(double d, int numOfDec)

Here you can find the source of roundDecimals(double d, int numOfDec)

Description

round Decimals

License

Apache License

Declaration

public static float roundDecimals(double d, int numOfDec) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.DecimalFormat;

public class Main {
    public static float roundDecimals(double d, int numOfDec) {
        if (numOfDec <= 0)
            return Math.round(d);
        String decFormatStr = "#.";
        for (int i = 0; i < numOfDec; i++)
            decFormatStr += "#";
        DecimalFormat decFormat = new DecimalFormat(decFormatStr);
        return Float.valueOf(decFormat.format(d)).floatValue();
    }/*from   w  w w .  j  a v  a  2s  .co  m*/
}

Related

  1. round(final double value)
  2. roundAlloc(Double alloc)
  3. roundAndTruncate(double rawValue, int precision)
  4. roundDecimal(double d, int radix)
  5. roundDecimalPlaces(final double input, final int decimalPlaces)
  6. roundDistanceFromMeterToKm(final double distanceInMeter)
  7. roundDouble(double iVal)
  8. roundDouble(double num, int decimal)
  9. roundDouble(double numero, int ceros_a_la_derecha)