logo

.NET Number and Currency Formatting

By Robbe Morris
Printer Friendly Version
View My Articles
636 Views
    

Custom Excel like auto formatting of numbers in .NET. Demonstrates how to take an entirely improperly formatted number and reformat it any way you like. Provides an alternative way to incorporate internationalization formatting for commas and decimals.


I recently published a JavaScript version of this number/currency reformatting code.  Some have asked for a server side version in .NET.

To see the formatting in action, JavaScript, Excel, Auto Format Numbers and International Currency.

Essentially, you pass in your comma delimiter, decimal delimiter, and number of places to the right of the decimal and the method takes care of the rest.  I use this single formatting method to handle percentages, research calculation indexes, currency, standard numerical formats, and various other unusual number format requests that may or may not fit well with the CultureInfo settings.

It can easily turn 123456789 into any of the following:

0.123456789
123.456789
1,234,567.89
-1,234.56789
123456789

It will also turn values that aren't real numbers into
properly formatted empty numbers.

0.000000000
0.0000
0

Feel free to tweak the code to add in custom rounding
or any other feature needed for your application.

Here's the code:

using System;
using System.Text;
using System.Text.RegularExpressions;
 

public class Utility
{

 public static double ToDouble(string originalValue,int roundPlaces)
 {

  double returnValue=0;

  try
  {

   originalValue = originalValue.Trim();

   if (originalValue.Length < 1)
   {
     return 0;
   }

   if (roundPlaces < 100)
   {
     returnValue = System.Math.Round(System.Double.Parse(originalValue),
                                     roundPlaces);
   }
   else { returnValue = System.Double.Parse(originalValue); }

  }
  catch  { return 0; }
  return returnValue;
 }
 

 public static string FormatEmptyNumber(string decimalDelimiter,
                                        int decimalPlaces)
 {

    string preDecimal = "0";
    string postDecimal = "";

    for (int i = 0; i < decimalPlaces; i++)
    {
      if (i == 0) { postDecimal += decimalDelimiter; }
      postDecimal += "0";
    }
    return preDecimal + postDecimal;

 }
     
 public static string FormatNumber(string value,
                                   string commaDelimiter,
                                   string decimalDelimiter,
                                   int decimalPlaces)
 {
    string minus = "";
    string preDecimal = "";
    string postDecimal = "";
    Regex regex = null;
    string returnValue = "";
    string pattern = "(-?[0-9]+)([0-9]{3})";

    try
    {

      value = value.Trim();

      if (decimalPlaces < 1) { decimalDelimiter = ""; }

      if (value.LastIndexOf("-") == 0)
      {
        minus = "-";
        value = value.Replace("-", "");
      }

      preDecimal = value;

      // preDecimal doesn't contain a number at all.
      // Return formatted zero representation.

      if (preDecimal.Length < 1)
      {
        return minus + FormatEmptyNumber(decimalDelimiter,
                                         decimalPlaces);
      }

      // preDecimal is 0 or a series of 0's.
      // Return formatted zero representation.

      if (commaDelimiter.Length > 0)
      {
        preDecimal = preDecimal.Replace(commaDelimiter, "");
      }
      if (decimalDelimiter.Length > 0)
      {
        preDecimal = preDecimal.Replace(decimalDelimiter, "");
      }

      if (ToDouble(preDecimal, 0) < 1)
      {
          return minus + FormatEmptyNumber(decimalDelimiter,
                                           decimalPlaces);
      }

      // predecimal has no numbers to the left.
      // Return formatted zero representation.

      if (preDecimal.Length == decimalPlaces)
      {
        return minus + "0" + decimalDelimiter + preDecimal;
      }

      // predecimal has fewer characters than the
      // specified number of decimal places.
      // Return formatted leading zero representation.

      if (preDecimal.Length < decimalPlaces)
      {
        if (decimalPlaces == 2)
        {
           return minus + FormatEmptyNumber(decimalDelimiter,
                                            decimalPlaces - 1)
                                             + preDecimal;
        }

        return minus + FormatEmptyNumber(decimalDelimiter,
                                         decimalPlaces - 2)
                                         + preDecimal;
      }

      // predecimal contains enough characters to
      // qualify to need decimal points rendered.
      // Parse out the pre and post decimal values
      // for future formatting.

      if (preDecimal.Length > decimalPlaces)
      {
         postDecimal = decimalDelimiter + preDecimal.Substring(
                                          preDecimal.Length - decimalPlaces);
         preDecimal = preDecimal.Substring(0, preDecimal.Length - decimalPlaces);
      }

      // Place comma oriented delimiter every 3 characters
      // against the numeric represenation of the "left" side
      // of the decimal representation.  When finished, return
      // both the left side comma formatted value together with
      // the right side decimal formatted value.

      regex = new Regex(pattern);

      while (Regex.IsMatch(preDecimal, pattern))
      {
         preDecimal = regex.Replace(preDecimal, "$1" + commaDelimiter + "$2");
      }

    }
    catch (Exception) { returnValue = ToDouble("0", 0).ToString(); }
    return minus + preDecimal + postDecimal;
 }
}




Biography - Robbe Morris
Robbe has been a Microsoft MVP in C# since 2004. He is also the co-founder of EggHeadCafe.com which provides .NET articles, book reviews, software reviews, and software download and purchase advice.


Didn't Find The Answer You Were Looking For?

EggHeadCafe has experts online right now that may know the answer to your question.  We pay them a bonus for answering as many questions as they can.  So, why not help them and yourself by becoming a member (free) and ask them your question right now?
Ask Question In Live Forum

If you have an OpenID and do not want to become a member of the EggHeadCafe forum, you can also sign on to Chat Chaos and post your question to our real time Silverlight chat application.
Ask Question In Chat Chaos

Article Discussion: .NET Number and Currency Formatting
Robbe Morris posted at Thursday, September 28, 2006 8:55 AM
Original Article
 

Currency Formatter
AsslamBasha P replied to Robbe Morris at Monday, October 16, 2006 7:44 AM

Hi

   I need to format the number as Currency like if the number is 120, then i need to convert it as $120.00. Please let me know how to do this from your code.

 

Regards
Asslam

 

Download the code and run the samples
Robbe Morris replied to AsslamBasha P at Monday, October 16, 2006 7:56 AM
Geez, all you have to do is call the method with the desired comma delimiter, desired decimal delimiter, and number of places to the right of the decimal.
 






  $1000 Contest    [)ia6l0 iii - $228  |  Jonathan VH - $161  |  Huggy Bear - $135  |  F Cali - $95  |  egg egg - $94  |  more Advertise  |  Privacy  |   (c) 2010