Ankush Chhabra
Ankush Chhabra

Reputation: 166

Decimal Format null handling

I am using Java 1.6 and we are using java.text.DecimalFormat to format numbers. For example

    DecimalFormat df = new DecimalFormat();
    df.setPositivePrefix("$");
    df.setNegativePrefix("(".concat($));
    df.setNegativeSuffix(")");
    df.setMaximumFractionDigits(2);
    df.setMinimumFractionDigits(2);
    df.setGroupingSize(3);

    df.format(new java.math.BigDecimal(100);

My application crash whenever pass null value to df.format(null)

Error: cannot format given object as a number 

My question is, how can I handle null value in df.format() function ?

I would like to pass null to df.format() function and would want it to return 0.00 instead of above error.

Thanks You

Regards,

Ankush

Upvotes: 4

Views: 9324

Answers (2)

Tobias Liefke
Tobias Liefke

Reputation: 9022

Extending DecimalFormat would break its API (correctly pointed out by Jon Skeet), but you could implement your own Format that wraps the given DecimalFormat:

public class OptionalValueFormat extends Format {

  private Format wrappedFormat;

  private String nullValue;

  public OptionalValueFormat(Format wrappedFormat, String nullValue) {
    this.wrappedFormat = wrappedFormat;
    this.nullValue = nullValue;
  }

  @Override
  public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
    if (obj == null) {
      // Just add our representation of the null value
      return toAppendTo.append(nullValue);
    }

    // Let the default format do its job
    return wrappedFormat.format(obj, toAppendTo, pos);
  }

  @Override
  public Object parseObject(String source, ParsePosition pos) {
    if (source == null || nullValue.equals(source)) {
      // Unwrap null
      return null;
    }

    // Let the default parser do its job
    return wrappedFormat.parseObject(source, pos);
  }

}

This wouldn't break the API of java.text.Format, as that one only requires toAppendTo and pos to be not null.

Example for the usage of OptionalValueFormat:

DecimalFormat df = ...

OptionalValueFormat format = new OptionalValueFormat(df, "0.00");
System.out.println(format.format(new java.math.BigDecimal(100)));
System.out.println(format.format(null));

Result:

100
0.00

Unfortunately none of the helper libraries I know offer such a format wrapper, so you will have to add this class to your project.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1502216

My application crash whenever pass null value to

Yes, it would. That's the documented behaviour:

Throws: IllegalArgumentException - if number is null or not an instance of Number.

Next:

I would like to pass null to df.format() function and would want it to return 0.00 instead of above error.

No, that's not going to work. It's documented not to work. Just don't pass null in... it's easy enough to detect. So you could use this:

String text = value == null ? "0.00" : df.format(value);

Or

String text = df.format(value == null ? BigDecimal.ZERO : value);

Upvotes: 11

Related Questions