Java Utililty Methods Double Number Format

List of utility methods to do Double Number Format

Description

The list of methods to do Double Number Format are organized into topic(s).

Method

StringformatDoubleWithPadding(String value, int length, char pad)
The total amount in 2 decimal places without decimal point with a leading zero.ie 12.00 = 1200 value = 000000146560
String valueStr = value + "";
int diff = length - valueStr.length();
StringBuilder strBuilder = new StringBuilder();
if (diff > 0) {
    while (diff > -1) {
        diff--;
        strBuilder.append(pad);
    strBuilder.append(valueStr);
    valueStr = strBuilder.toString();
    valueStr = valueStr.substring(0, valueStr.indexOf('.'))
            + valueStr.substring(valueStr.indexOf('.') + 1, valueStr.length());
return valueStr;
DecimalFormatgetFormat(double d)
_more_
d = Math.abs(d);
if ((d > 10000) || (d == 0)) {
    return FORMATS[0];
if (d > 1000) {
    return FORMATS[1];
if (d > 100) {
...
DecimalFormatgetFormat(double value)
get Format
DecimalFormat format;
if (value < 10) {
    format = new DecimalFormat("#.##");
} else if (value < 100) {
    format = new DecimalFormat("#.#");
} else {
    format = new DecimalFormat("#");
format.setRoundingMode(RoundingMode.HALF_UP);
return format;
StringgetFormatAmount(Double d)
get Format Amount
if (d == null) {
    return "0.00";
DecimalFormat df = new DecimalFormat("0.00");
return df.format(d);
StringgetFormattedNumber(Double number, int decimals, Locale locale)
get Formatted Number
NumberFormat nf = NumberFormat.getInstance(locale);
nf.setMaximumFractionDigits(decimals);
nf.setMinimumFractionDigits(decimals);
return nf.format(number);
StringgetFormattedString(double d, int numDecimalPlaces)
get Formatted String
StringBuilder sb = new StringBuilder();
sb.append("##.");
for (int i = 0; i < numDecimalPlaces; i++)
    sb.append("#");
DecimalFormat df = new DecimalFormat(sb.toString());
return df.format(d);
StringgetFormattedTwoDecimal(double d)
get Formatted Two Decimal
DecimalFormat df = new DecimalFormat("##.##");
return df.format(d);
StringgetFormattedValue(double argDoubleValue)
This method will convert double value into US Locale Currency value in String
BigDecimal empClaims = new BigDecimal(argDoubleValue);
NumberFormat numFormat = NumberFormat.getCurrencyInstance(Locale.US);
String empFormatted = numFormat.format(empClaims);
return empFormatted;
StringgetTwoDecimalByDecimalFormat(double number)
get Two Decimal By Decimal Format
String decimal = "";
java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");
decimal = df.format(number);
return decimal;
StringgetValidString(Double value, DecimalFormat formatter)
get Valid String
String str = "";
if (value == null || Double.isNaN(value))
    str = "NaN";
else
    str = formatter.format(value) + "";
return str;