Link to home
Start Free TrialLog 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
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial