Introduction
Number and date formatting are issues consuming time and efforts, while things should not be so complicated ...
Background
These code snippets are slightly modified versions of the code snippets found on MSDN. One learns better by doing instead of studying long explanations and theory explanation ... especially when the activity in question is coding.
Using the Code
Just create a new project, copy paste the whole code, run it with F5 and try to change some of the parameters. It is important to realize that there are different set of cultures ( usually one for a country ), which define the formatting of numbers and dates, however C# gives the developer to override some of the specific formattings in the chosen culture.
using System;
using System.Globalization;
class Sample
{
enum Color { Yellow = 1, Blue, Green };
static DateTime thisDate = DateTime.Now;
public static void Main()
{
string s = "";
Console.Clear();
CultureInfo cultureToUse = new CultureInfo("fi-FI");
Console.WriteLine("Using the following CultureInfor " +
cultureToUse.Name);
cultureToUse.NumberFormat.CurrencyDecimalDigits = 3;
cultureToUse.NumberFormat.NumberDecimalDigits = 3;
cultureToUse.NumberFormat.NumberGroupSeparator = " ";
cultureToUse.NumberFormat.CurrencySymbol = "euro";
cultureToUse.NumberFormat.NumberDecimalSeparator = ",";
Console.WriteLine("Standard Numeric Format Specifiers");
s = String.Format( cultureToUse ,
"(C) Currency: . . . . . . . . {0:C}\n" +
"(D) Decimal:. . . . . . . . . {0:D}\n" +
"(E) Scientific: . . . . . . . {1:E}\n" +
"(F) Fixed point:. . . . . . . {1:F}\n" +
"(F) Floating point:. . . . . .{2:F5}\n" +
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(N) Number: . . . . . . . . . {0:N2}\n" +
"(P) Percent:. . . . . . . . . {1:P}\n" +
"(R) Round-trip: . . . . . . . {1:R}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
123456789 , -123.45f , 123456789.123456789);
Console.WriteLine(s);
Console.WriteLine("\n Using as an input a string");
double aDouble = System.Convert.ToDouble("123456789,123456789");
s = String.Format(cultureToUse,
"{0:N3}" , aDouble);
Console.WriteLine(s);
s = String.Format(cultureToUse,
"(N) Number: . . . . . . . . . {0:N5}\n" +
123456789.123456789, 11111111111111111.123456789);
Console.WriteLine(s);
double d = 123456789.2345678901234567890;
Console.WriteLine("Floating-Point:\t{0:F16}", d);
Console.WriteLine("When using the toString Methods" + d.ToString("{0:F16}",
cultureToUse));
Console.WriteLine("Standard DateTime Format Specifiers");
s = String.Format(cultureToUse ,
"(d) Short date: . . . . . . . {0:d}\n" +
"(D) Long date:. . . . . . . . {0:D}\n" +
"(t) Short time: . . . . . . . {0:t}\n" +
"(T) Long time:. . . . . . . . {0:T}\n" +
"(f) Full date/short time: . . {0:f}\n" +
"(F) Full date/long time:. . . {0:F}\n" +
"(g) General date/short time:. {0:g}\n" +
"(G) General date/long time: . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(M) Month:. . . . . . . . . . {0:M}\n" +
"(R) RFC1123:. . . . . . . . . {0:R}\n" +
"(s) Sortable: . . . . . . . . {0:s}\n" +
"(u) Universal sortable: . . . {0:u} (invariant)\n" +
"(U) Universal sortable: . . . {0:U}\n" +
"(Y) Year: . . . . . . . . . . {0:Y}\n",
thisDate);
Console.WriteLine(s);
Console.WriteLine("Standard Enumeration Format Specifiers");
s = String.Format(cultureToUse ,
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
"(D) Decimal number: . . . . . {0:D}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
Color.Green);
Console.WriteLine(s);
System.Threading.Thread.Sleep(15000);
}
}
Bonus Code
The implementation as a static method for nice Finnish number formatting (well you could change your culture since you now know how) and a couple of static methods for date conversation.
Just create a sample DateConverter
class, copy paste the methods in it and use them as DateConverter.FromObjToLongDateString( valueObject );
public static string FormatDecimalNumber ( bool flagNeedsFormatting ,
ref string filledValue )
{
if (flagNeedsFormatting == false)
return filledValue;
else if (filledValue !=null && flagNeedsFormatting &&
filledValue.Equals ( String.Empty ) == false &&
filledValue.Equals ( " " ) == false)
{
CultureInfo cultureToUse = new CultureInfo ( "fi-FI" );
cultureToUse.NumberFormat.CurrencyDecimalDigits = 3;
cultureToUse.NumberFormat.NumberDecimalDigits = 3;
cultureToUse.NumberFormat.NumberGroupSeparator = " ";
double doubleFilledValue = System.Convert.ToDouble ( filledValue );
filledValue = String.Format ( cultureToUse , "{0:N3}" , doubleFilledValue );
return filledValue;
}
else
return filledValue;
}
public static string FromObjToShortDateString ( object filledValue )
{
string strTime = System.Convert.ToString ( filledValue ).Trim ();
CultureInfo culture = CultureInfo.CreateSpecificCulture ( "fi-FI" );
DateTime dtDate = DateTime.Parse ( strTime , culture );
return dtDate.ToShortDateString ();
}
public static string FromObjToLongDateString ( object filledValue )
{
string strTime = System.Convert.ToString ( filledValue ).Trim ();
CultureInfo culture = CultureInfo.CreateSpecificCulture ( "fi-FI" );
DateTime dtDate = DateTime.Parse ( strTime , culture );
return dtDate.ToLongTimeString ();
}
Points of Interest
A how-to create, truely global, web-based application. There should be a UserSettings table in db, storing the specific culture info for each user, but for better usability the users should be given the possibility to change individual culture specific metacharacters, such as a decimal separator, group separators etc.