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

doublegetRoundedValue(double doubleValue)
Rounds off the double value up to 2 decimal digits.
DecimalFormat decimalFormatter = new DecimalFormat(ROUND_UPTO_TWO_DECIMAL_FORMAT);
double roundedValue = Double.parseDouble(decimalFormatter.format(doubleValue));
return roundedValue;
StringgetRoundedValue(double value)
Get rounded double value, according to the value of the double.
StringBuffer decimalFormatBuffer = new StringBuffer(TWO_DECIMALS_FORMAT);
double compareValue = 0.1;
while (value > 0 && value < compareValue && !decimalFormatBuffer.toString().equals(MAX_DECIMALS_FORMAT)) {
    decimalFormatBuffer.append(DECIMALS_FORMAT_TOKEN);
    compareValue = compareValue * 0.1;
DecimalFormat decimalFormat = new DecimalFormat(decimalFormatBuffer.toString());
return decimalFormat.format(value);
...
Stringround(double d, int n)
round
if (d == 0) {
    d = 0;
if (n < 0) {
    n = 0;
String str = "";
if (n == 0) {
...
doubleround(double d, int p)
Round a double to p decimal places
String format = "0.";
for (int i = 0; i < p; i++)
    format += "0";
DecimalFormat twoDForm = new DecimalFormat(format, new DecimalFormatSymbols(Locale.US));
return Double.valueOf(twoDForm.format(d));
doubleround(double dValue)
round
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
double fractionMultiplier = Math.pow(10.0, currencyFormat.getMaximumFractionDigits());
return Math.rint(dValue * fractionMultiplier) / fractionMultiplier;
Stringround(double number)
Rounds the given number to two decimals.
DecimalFormat df = new DecimalFormat("#.##");
df.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.US));
df.setRoundingMode(RoundingMode.HALF_UP);
return df.format(number);
Stringround(double unrounded, int precision)
round
DecimalFormat df = new DecimalFormat("####0.##########");
BigDecimal bd = new BigDecimal(unrounded);
BigDecimal rounded = bd.setScale(precision, BigDecimal.ROUND_HALF_UP);
return df.format(rounded.doubleValue());
Stringround(final double value)
round
return FORMAT_PRECISION_2.get().format(value);
StringroundAlloc(Double alloc)
round Alloc
return new DecimalFormat("#.#").format(alloc);
StringroundAndTruncate(double rawValue, int precision)
round And Truncate
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(precision);
df.setMinimumFractionDigits(precision);
df.setMinimumIntegerDigits(1);
return df.format(rawValue);