ASP.NET 中的特定文化设置的数字格式化






4.33/5 (5投票s)
介绍了应用自定义数字格式化的基础知识。
引言
数字和日期格式化会占用时间和精力,而事情本不应该如此复杂……
背景
这些代码片段是对 MSDN 上找到的代码片段的略微修改版本。比起研究冗长的解释和理论说明,动手实践更能学到东西……特别是当涉及编码时。
Using the Code
只需创建一个新项目,复制粘贴整个代码,使用 F5 运行它,并尝试更改一些参数。重要的是要认识到,存在不同的文化设置(通常一个国家/地区对应一个),它们定义了数字和日期的格式;但是,C# 允许开发人员覆盖所选文化设置中的一些特定格式。
// This code example demonstrates the String.Format() method.
// Formatting for this example uses the "fi-FI" 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();
//Create first the format provider the String.Format
//will use to format our string
CultureInfo cultureToUse = new CultureInfo("fi-FI");
Console.WriteLine("Using the following CultureInfor " +
cultureToUse.Name);
//Now practice some decimal numbers
//Here we override the culture specific formattings
cultureToUse.NumberFormat.CurrencyDecimalDigits = 3;
cultureToUse.NumberFormat.NumberDecimalDigits = 3;
cultureToUse.NumberFormat.NumberGroupSeparator = " ";
cultureToUse.NumberFormat.CurrencySymbol = "euro";
cultureToUse.NumberFormat.NumberDecimalSeparator = ",";
// Format a negative integer or floating-point number in various ways.
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);
//"(F) Fixed point:. . . . . . . {0:F}\n" +
//"(F) Floating point:. . . . . .{0:F3}\n" +
// 122 , aDouble);
Console.WriteLine(s);
s = String.Format(cultureToUse,
// "(D) Decimal:. . . . . . . . . {0:D}\n" +
// "(F) Fixed point:. . . . . . . {0:F5}\n" +
"(N) Number: . . . . . . . . . {0:N5}\n" +
// "(F) Fixed point:. . . . . . . {0:F}\n" +
// "(F) Floating point:. . . . . . . {0:F3}\n" +
123456789.123456789, 11111111111111111.123456789);
Console.WriteLine(s);
double d = 123456789.2345678901234567890;
Console.WriteLine("Floating-Point:\t{0:F16}", d); // 1.2345678901234600
Console.WriteLine("When using the toString Methods" +
d.ToString("{0:F16}", cultureToUse));
// Format the current date in various ways.
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);
// Format a Color enumeration value in various ways.
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);
}
}
附加代码
此实现是一个静态方法,用于漂亮的芬兰数字格式化(当然,既然你现在知道如何操作,可以更改你的文化设置)。还有一对用于日期转换的静态方法。
只需创建一个示例 DateConverter
类,将方法复制粘贴到其中,并像这样使用它们:DateConverter.FromObjToLongDateString( valueObject );
。
public static string FormatDecimalNumber ( bool flagNeedsFormatting ,
ref string filledValue )
{
//E.G. pass CultureInfo specifig string
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;
} //eof if needs Formatting
else
return filledValue;
} //eof method FormatDecimalNumber
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 ();
} //eof method FromObjToShortDateString ( object o )
public static string FromObjToLongDateString ( object filledValue )
{
string strTime = System.Convert.ToString ( filledValue ).Trim ();
//change fi-FI Finland to de-DE for German etc.
CultureInfo culture = CultureInfo.CreateSpecificCulture ( "fi-FI" );
DateTime dtDate = DateTime.Parse ( strTime , culture );
return dtDate.ToLongTimeString ();
} //eof method FromObjToLongDateString ( object filledValue )
关注点
这是创建真正全球化的 Web 应用程序的方法。数据库中应该有一个 UserSettings 表,存储每个用户的特定文化信息,但为了更好的可用性,应该允许用户更改单个文化特定的元字符,例如小数分隔符、分组分隔符等。