views:

1609

answers:

4

I would like to know the easiest way to format a string as accounting style. I know how to format as currency using {0:c} but there are some differences in accounting style, for example, all the dollar signs will line up as well as all the decimal points, and negatives are expressed in parenthesis rather than with a "-" minus sign. You can find a good example of the way i would like it in excel if you format the cells as "accounting" with 2 decimal places.

+2  A: 

There's no format string shortcut (the single-character ones with default rules) for handling accounting style formats (here's a cheat sheet with the available format strings) so you'll have to write a more specific one (like Patrick's answer) or your own parsing method.

The alignment requirements would be specific to how you're displaying them. I'm assuming you are using a table, in which case you're limited by what HTML supports, and it doesn't support accounting style alignments like Excel.

John Sheehan
+1 - I was just looking for a printable list of format strings
John Rasch
Ya, props on that one!
Gavin Miller
+3  A: 

Ignoring your alignment requirements, you could use

number.ToString("€#,##0.00;(€#,##0.00);Zero")

to bracket negative numbers.

To align your numbers, you'd have to format without the currency symbol, and pad the formatted numbers yourself with spaces, using a fixed width font would make this job easier for you.

EDIT:

It seems String.Format is your friend:

String.Format("{0,15:#,##0.00 ;(#,##0.00);-   }", number)

where 15 is the total width of the output, and you need to append this text to your currency symbol. (Again, this aligns in fixed width only)

Patrick McDonald
+1  A: 

In this blog there were some various formats outlined and this one seemed to be close to what you were looking for:

int neg = -10;
int pos = 10;
// C or c (Currency): It represent how many decimal place of zeros to show.
String.Format("{0:C4}", pos);      //"$10.0000"
String.Format("{0:C4}", neg);      //"($10.0000)"

It doesn't handle the padding (you may have to fix that yourself), but it does have the proper parenthesis.

Dillie-O
This is culture-specific and may not work with every culture.
John Sheehan
+1  A: 

You could do something using a variation of Patricks method. This will handle formating and alignment assuming you know the upper bound of how large a value you are dealing with:

private static string OutputAsCur(decimal val)
{
    string format = "  #,##0.00 ; (#,##0.00);Zero";
    string frmt = val.ToString(format);
    return CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol + frmt.PadLeft(15, ' ');
}

Here's a simple example app to see it format:

    static void Main(string[] args)
    {
        decimal d = 155.55m;

        Console.WriteLine(OutputAsCur(d));
        Console.WriteLine(OutputAsCur(d * -1));
        Console.WriteLine(OutputAsCur(1002.32m));
        Console.WriteLine(OutputAsCur(1002.32m * -1));
        Console.ReadLine();
     }
JoshBerke