C#学习笔记:字符串格式化为货币和日期

参考书目: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();
        }
    }
}

运行结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值