Link to home
Create AccountLog in
Avatar of daveokst
daveokst

asked on

Formatting big numbers in c#

Our client is dealing with cash accounts that are in the $$$ millions.  We are using a grid to display the balances and other totals in a number of columns that span the page.  Since the columns can get narrow, the client would like large numbers to be formatted in an abbreviated way.  For example...

$25,000,000.00 would be formatted as $25.0M or something to that effect.  Is there a common formatter in the .Net framework that accommodates formatting large number in this way?

Thanks!

Code example preferred.
Avatar of joechina
joechina

You can write your own formatprovider class.

Example:
http://msdn.microsoft.com/en-us/library/system.iformatprovider.aspx

In your formatprovider, just divide the number by 1000000, then format the result to currency and add 'M' at the end.
The closest built-in format that comes to my mind is the "e" or scientific notation format.

An example:
double value = 25000000;
string f = value.ToString("e2");
Console.WriteLine(f);
//outputs: 2.50e+007

If the client is used to looking at spreadsheets, they may be used to this sort of notation.

-Jason
ASKER CERTIFIED SOLUTION
Avatar of daveokst
daveokst

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer