参考书目:C#6.0学习笔记——从第一行C#代码到第一个项目设计(作者周家安)P162
学习目标:学习字符串格式化为货币和日期的方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Example5_15
{
class Program
{
static void Main(string[] args)
{
//中文英文区域信息
System.Globalization.CultureInfo en_cul, zh_cul;
en_cul = System.Globalization.CultureInfo.GetCultureInfo("en");
zh_cul = System.Globalization.CultureInfo.GetCultureInfo("zh");
System.Globalization.CultureInfo ja_cul = System.Globalization.CultureInfo.GetCultureInfo("ja");
//双精度数值转化为货币字符串
//1. 货币
Console.WriteLine("----货币格式----");
double dcur = 199.27;
//下面代码中的C2表示吧数值转化为货币格式的字符串,并保留两位小数
Console.WriteLine("拥有{0}的货币格式:{1}",
zh_cul.DisplayName, dcur.ToString("C2", zh_cul));
Console.WriteLine("拥有{0}的货币格式:{1}",
en_cul.DisplayName, dcur.ToString("C2", en_cul));
//2.将数值转化为用逗号分隔的数字字符串,每三位数为一组
Console.WriteLine("----数字格式----");
double dnv = 123456.313;
Console.WriteLine("拥有{0}的数字格式:{1}",
zh_cul.DisplayName, dnv.ToString("N", zh_cul));
Console.WriteLine("拥有{1}的数字格式:{1}",
en_cul.DisplayName, dnv.ToString("N", zh_cul));
//----------------时间---------
// 用于测试的时间
DateTime dt = new DateTime(2014, 8, 9, 17, 53, 40);
string strFormat = ""; //格式
/*------------------------------------- 标准格式 -----------------------------------*/
Console.WriteLine("************************* 标准格式 *************************");
// 长日期
strFormat = "D";
Console.WriteLine("---------- 长日期 ----------");
Console.WriteLine("{0,-8}{1}", zh_cul.DisplayName, dt.ToString(strFormat, zh_cul));
Console.WriteLine("{0,-8}{1}", en_cul.DisplayName, dt.ToString(strFormat, en_cul));
Console.WriteLine("{0,-8}{1}", ja_cul.DisplayName, dt.ToString(strFormat, ja_cul));
// 短日期
strFormat = "d";
Console.WriteLine("\n---------- 短日期 ----------");
Console.WriteLine("{0,-8}{1}", zh_cul.DisplayName, dt.ToString(strFormat, zh_cul));
Console.WriteLine("{0,-8}{1}", en_cul.DisplayName, dt.ToString(strFormat, en_cul));
Console.WriteLine("{0,-8}{1}", ja_cul.DisplayName, dt.ToString(strFormat, ja_cul));
// 完整格式(长时间)
strFormat = "F";
Console.WriteLine("\n---------- 完整格式(长时间) ----------");
Console.WriteLine("{0,-8}{1}", zh_cul.DisplayName, dt.ToString(strFormat, zh_cul));
Console.WriteLine("{0,-8}{1}", en_cul.DisplayName, dt.ToString(strFormat, en_cul));
Console.WriteLine("{0,-8}{1}", ja_cul.DisplayName, dt.ToString(strFormat, ja_cul));
// 完整格式(短时间)
strFormat = "f";
Console.WriteLine("\n---------- 完整格式(短时间) ----------");
Console.WriteLine("{0,-8}{1}", zh_cul.DisplayName, dt.ToString(strFormat, zh_cul));
Console.WriteLine("{0,-8}{1}", en_cul.DisplayName, dt.ToString(strFormat, en_cul));
Console.WriteLine("{0,-8}{1}", ja_cul.DisplayName, dt.ToString(strFormat, ja_cul));
/*-----------------------------------自定义格式------------------------------------------*/
/*------------------------------------- 自定义格式 -----------------------------------*/
Console.WriteLine("\n\n*********************** 自定义格式 *************************");
// 一周中的一天的全称
strFormat = "dddd";
Console.WriteLine("-------- 一周中某天的全称 --------");
Console.WriteLine("{0,-8}{1}", zh_cul.DisplayName, dt.ToString(strFormat, zh_cul));
Console.WriteLine("{0,-8}{1}", en_cul.DisplayName, dt.ToString(strFormat, en_cul));
Console.WriteLine("{0,-8}{1}", ja_cul.DisplayName, dt.ToString(strFormat, ja_cul));
// 一个月中的一天
strFormat = "%d";
Console.WriteLine("\n-------- 一个月中的一天 --------");
Console.WriteLine("{0,-8}{1}", zh_cul.DisplayName, dt.ToString(strFormat, zh_cul));
Console.WriteLine("{0,-8}{1}", en_cul.DisplayName, dt.ToString(strFormat, en_cul));
Console.WriteLine("{0,-8}{1}", ja_cul.DisplayName, dt.ToString(strFormat, ja_cul));
// 某个月的全称
strFormat = "MMMM";
Console.WriteLine("\n-------- 某个月的全称 --------");
Console.WriteLine("{0,-8}{1}", zh_cul.DisplayName, dt.ToString(strFormat, zh_cul));
Console.WriteLine("{0,-8}{1}", en_cul.DisplayName, dt.ToString(strFormat, en_cul));
Console.WriteLine("{0,-8}{1}", ja_cul.DisplayName, dt.ToString(strFormat, ja_cul));
// 自定义日期时间格式
strFormat = "yyyy年M月d日 HH时mm分ss秒";
Console.WriteLine("\n------ 自定义日期时间格式 ------");
Console.WriteLine("自定义日期时间格式:" + dt.ToString(strFormat));
Console.ReadKey();
}
}
}
运行结果: