Java Utililty Methods Decimal Round

List of utility methods to do Decimal Round

  1. HOME
  2. Java
  3. D
  4. Decimal Round

Description

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

Method

double getRoundedValue(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;
String getRoundedValue(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);
...
String round(double d, int n)
round
if (d == 0) {
 d = 0;
if (n < 0) {
 n = 0;
String str = "";
if (n == 0) {
...
double round(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));
double round(double dValue)
round
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
double fractionMultiplier = Math.pow(10.0, currencyFormat.getMaximumFractionDigits());
return Math.rint(dValue * fractionMultiplier) / fractionMultiplier;
String round(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);
String round(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());
String round(final double value)
round
return FORMAT_PRECISION_2.get().format(value);
String roundAlloc(Double alloc)
round Alloc
return new DecimalFormat("#.#").format(alloc);
String roundAndTruncate(double rawValue, int precision)
round And Truncate
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(precision);
df.setMinimumFractionDigits(precision);
df.setMinimumIntegerDigits(1);
return df.format(rawValue);


AltStyle によって変換されたページ (->オリジナル) /