Wednesday, September 22, 2010

Format Negative Numbers

Just a simple little tip this week. I wanted to format numeric data with a custom format. I wanted positive numbers to look like this relatively standard format


But I also wanted negative numbers to show up like:

 1,245.00 -

With the negative sign showing to the right of the number. 

At first this was a  this is a bit of a problem.  I figured out a way to do it using OnRender script, but it was kind of ugly.  Turns out the solution is quite a bit more simple. BIRT is using the java DecimalFormat for numeric data in the CustomFormat.  One of the accepted patterns for DecimalFormat is in the form of

 PositivePattern ; NegativePattern

So all you need to do is select Custom Format and enter the format as (see below)

 #,##0.00; #,###.00 -


Anonymous said...

Excellent and concise solution to this problem. Thank you for posting.

H.M. said...

Thank you for this! Was using a longer workaround with Formatter.format and dynamic text, but this is much simpler.