Java Utililty Methods Decimal Round

List of utility methods to do Decimal Round

Description

The list of methods to do Decimal Round are organized into topic(s).

Method

doubleroundDecimal(double d, int radix)
Zaokragla wartosc, do wyznaczonego miejsca po przecinku
NumberFormat format = NumberFormat.getInstance();
format.setGroupingUsed(false);
format.setMaximumFractionDigits(radix);
double res;
try {
    res = format.parse(format.format(d)).doubleValue();
} catch (ParseException e) {
    res = Double.NaN;
...
StringroundDecimalPlaces(final double input, final int decimalPlaces)
round Decimal Places
if (decimalPlaces <= 0) {
    return NORMAL.format(Math.round(input));
final StringBuilder format = new StringBuilder("###0" + DS);
final StringBuilder negativeZero = new StringBuilder("-0" + DS);
for (int i = 0; i < decimalPlaces; i++) {
    format.append('0');
    negativeZero.append('0');
...
floatroundDecimals(double d, int numOfDec)
round Decimals
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();
StringroundDistanceFromMeterToKm(final double distanceInMeter)
z.B.
final double distanceInKilometer = distanceInMeter / KILOMETER_IN_METER;
final DecimalFormat format = new DecimalFormat("0.000"); 
final DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setDecimalSeparator('.');
format.setDecimalFormatSymbols(dfs);
return format.format(distanceInKilometer);
doubleroundDouble(double iVal)
round Double
DecimalFormat df = new DecimalFormat(".0");
String tmp = df.format(iVal);
return Double.parseDouble(tmp);
StringroundDouble(double num, int decimal)
round Double
String s = "0.";
for (int i = 0; i < decimal; i++) {
    s += "0";
NumberFormat f = new DecimalFormat(s);
s = f.format(num);
return s;
StringroundDouble(double numero, int ceros_a_la_derecha)
round Double
String mascara = "#.";
String formateado = "";
String num = "";
for (int i = 0; i < ceros_a_la_derecha; i++) {
    mascara += "0";
NumberFormat formatter = new DecimalFormat(mascara);
num = "" + formatter.format(numero);
...
doubleroundDouble(Double val, int precision)
round Double
Double dVal = adjDecimalSep(val);
BigDecimal bigNumber1 = new BigDecimal(dVal, MathContext.UNLIMITED); 
return bigNumber1.setScale(precision, BigDecimal.ROUND_CEILING).doubleValue();
StringroundedNumber(double inVal)
Format a number to a sensible precision
int numDigits = 0;
if (inVal < 1.0)
    numDigits = 3;
else if (inVal < 10.0)
    numDigits = 2;
else if (inVal < 100.0)
    numDigits = 1;
FORMAT_FLEX.setMaximumFractionDigits(numDigits);
...
doubleRoundFractionalTons(double d)
Round Fractional Tons
return Double.valueOf(dft.format(d));