Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accounting Style string format in ASP.NET

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.

like image 493
Russ Bradberry Avatar asked Mar 30 '09 17:03

Russ Bradberry


People also ask

What is the 0.00 format?

Zero (0) is used to force the display of insignificant zeros when a number has fewer digits than than zeros in the format. For example, the custom format 0.00 will display zero as 0.00, 1.1 as 1.10 and .

What is .NET format?

. NET defines a set of standard format specifiers for all numeric types, all date and time types, and all enumeration types. For example, each of these categories supports a "G" standard format specifier, which defines a general string representation of a value of that type.

Which of the following package classes is used to format numeric output?

The DecimalFormat Class You can use the java. text. DecimalFormat class to control the display of leading and trailing zeros, prefixes and suffixes, grouping (thousands) separators, and the decimal separator.


3 Answers

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)

like image 156
Patrick McDonald Avatar answered Oct 08 '22 02:10

Patrick McDonald


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.

like image 26
John Sheehan Avatar answered Oct 08 '22 02:10

John Sheehan


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.

like image 33
Dillie-O Avatar answered Oct 08 '22 02:10

Dillie-O