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

doubleroundNumDecimals(double d, int num)
round Num Decimals
StringBuilder format = new StringBuilder("#.");
for (int i = 0; i < num; i++) {
    format.append("#");
DecimalFormat f = new DecimalFormat(format.toString());
return Double.valueOf(f.format(d));
doubleroundOffToTwoDecimal(double value)
round Off To Two Decimal
DecimalFormat df = new DecimalFormat("0.00");
return new Double(df.format(value));
doubleroundThreeDecimals(double d)
round Three Decimals
if (Double.isInfinite(d) || Double.isNaN(d)) {
    return d;
DecimalFormat threeDForm = new DecimalFormat("#.###");
return Double.valueOf(threeDForm.format(d));
DoubleroundTwoDecimals(double d)
This method rounds up a decimal number up to N decimals.
DecimalFormat twoDForm = new DecimalFormat("#.##");
return Double.valueOf(twoDForm.format(d));
doubleroundTwoDecimals(double d)
round Two Decimals
try {
    DecimalFormat twoDForm = new DecimalFormat("#.##");
    return Double.valueOf(twoDForm.format(d));
} catch (NumberFormatException e) {
    return d;
DoubleroundTwoDecimals(Double d)
Round up to two decimals
DecimalFormat twoDForm = new DecimalFormat("###.##");
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setDecimalSeparator('.');
twoDForm.setDecimalFormatSymbols(dfs);
return Double.valueOf(twoDForm.format(d));
StringroundTwoDecimals(double d)
round Two Decimals
DecimalFormat twoDForm = new DecimalFormat("#.##");
return twoDForm.format(d);
Stringto_decimal(double v)
Returns a decimal representation of a double (no scientific notation used).
final NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(4);
return nf.format(v);
StringtoLimitDecimalFloatStr(double number, int newScale)
to Limit Decimal Float Str
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(newScale);
return nf.format(number);
StringtoNumber(Double value, Double defaultValue)
Remove decimals
String number = "";
if (value == null) {
    value = defaultValue;
if (value != null) {
    NumberFormat nf = NumberFormat.getInstance();
    nf.setGroupingUsed(false);
    nf.setMaximumFractionDigits(0);
...