• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎(chǔ)} {C++ 高級} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

            IFormattable,IFormatProvider,ICustomFormatter 接口的實(shí)現(xiàn)

            .NET Framework提供了方法,能夠?qū)⑷魏螖?shù)值、枚舉以及日期和時(shí)間等基數(shù)據(jù)類型表示為字符串
            格式化由格式說明符字符的字符串控制,該字符串指示如何表示基類型值
            例如,格式說明符指示:是否應(yīng)該用科學(xué)記數(shù)法來表示格式化的數(shù)字
            例如:格式字符"C",說明貨幣格式

            同時(shí).NET Framework還使用區(qū)域性設(shè)置,以便用適合于特定區(qū)域性的形式表示基類型。
            我們可以提供自定義的區(qū)域性設(shè)置,或者使用與當(dāng)前線程關(guān)聯(lián)的默認(rèn)區(qū)域性設(shè)置。
            例如,格式化貨幣類型的時(shí)候,區(qū)域性設(shè)置指定用于貨幣符號

            要是我們想擁有自己定義的格式化,.NET Framework也允許我們定義自己格式化方案和自定義區(qū)域性設(shè)置。
            例如:我想格式字符"MyFormat",來說明我自定義的格式,即在字符前加三個(gè)***

            關(guān)于數(shù)字格式字符串,可以參考類
            System.Globalization.NumberFormatInfo
            關(guān)于日期與時(shí)間格式字符串,可以參考類
            System.Globalization.DateTimeFormatInfo

            先看看IFormattable接口的原型
            public interface IFormattable
            {
            ????? // Methods
            ????? string ToString(string format, IFormatProvider formatProvider);
            }
            參數(shù)說明:
            format
            指定要使用的格式的 String
            當(dāng)為空引用時(shí),表示使用為 IFormattable 實(shí)現(xiàn)的類型定義的默認(rèn)格式
            formatProvider
            用于格式化該值的 IFormatProvider
            當(dāng)為空引用時(shí),從操作系統(tǒng)的當(dāng)前區(qū)域設(shè)置中獲取格式信息的

            一些基本的值類型實(shí)現(xiàn)了該接口,例如:
            Int32 ,UInt32 , DateTime ,Guid ,類Enum

            再看看IFormatProvider接口的原型
            public interface IFormatProvider
            {
            ????? // Methods
            ????? object GetFormat(Type formatType);
            }
            參數(shù)說明:
            formatType
            一個(gè)對象,它指定要獲取的格式對象的類型

            NumberFormatInfo、DateTimeFormatInfo和CultureInfo實(shí)現(xiàn)IFormatProvider接口

            NumberFormatInfo提供數(shù)字格式信息,如用于小數(shù)分隔符和千位分隔符的字符,以及貨幣值中貨幣符號的拼寫和位置
            DateTimeFormatInfo提供與日期相關(guān)和與時(shí)間相關(guān)的格式信息,如日期模式中月、日和年的位置
            CultureInfo包含特定區(qū)域性中的默認(rèn)格式信息,其中包括數(shù)字格式信息以及與日期相關(guān)和與時(shí)間相關(guān)的格式信息

            再看看ICustomFormatter接口的原型
            public interface ICustomFormatter
            {
            ????? // Methods
            ????? string Format(string format, object arg, IFormatProvider formatProvider);
            }
            參數(shù)說明:
            format
            包含格式規(guī)范的格式字符串
            arg
            要格式化的對象
            formatProvider
            一個(gè) IFormatProvider 對象,它提供有關(guān)當(dāng)前實(shí)例的格式信息


            在arg為空引用時(shí),引發(fā)異常
            如果 format 為空引用 ,將使用默認(rèn)格式規(guī)范
            如果 formatProvider 為空引用 ,則忽略該參數(shù)

            好了,說了這么多
            我們來動(dòng)手來實(shí)現(xiàn)格式字符"MyFormat",在字符前加三個(gè)***的需求

            定義一個(gè)類

            using ?System;

            namespace ?MyFormat
            {
            ????
            public ? class ?MyClass?:?System.IFormattable
            ????
            {
            ????????Double?d;

            ????????
            public ?MyClass(Double?d)
            ????????
            {
            ????????????
            this .d = d;
            ????????}


            ????????
            public ? string ?ToString( string ?format,?IFormatProvider?formatProvider)
            ????????
            {
            ????????????
            return ?(format == " MyFormat " ) ? " *** " + d.ToString(formatProvider):d.ToString(format,formatProvider);
            ????????}

            ????}

            }
            再到一控制臺中
            System.Globalization.CultureInfo?culture=null;

            ????????????MyClass?myClass
            =new?MyClass(5);
            ????????????
            //當(dāng)IFormatProvider為空時(shí),調(diào)用的是當(dāng)前線程關(guān)聯(lián)的文化信息
            ????????????Console.WriteLine("顯示中國貨幣格式:{0}",myClass.ToString("C",null));

            ????????????culture
            =System.Globalization.CultureInfo.CurrentCulture;
            ????????????Console.WriteLine(
            "顯示當(dāng)前系統(tǒng)默認(rèn)貨幣格式:{0}",myClass.ToString("C",culture));

            ????????????culture
            =new?System.Globalization.CultureInfo("zh-HK");
            ????????????Console.WriteLine(
            "顯示香港特別行政區(qū)貨幣格式:{0}",myClass.ToString("C",culture));

            ????????????Console.WriteLine(
            "顯示我自己定義的貨幣格式:{0}",myClass.ToString("MyFormat",null));
            ????????????
            ????????????Console.ReadLine();

            效果如下:


            如果希望自定義格式化能在多個(gè)不同類使用,那么實(shí)現(xiàn)我們應(yīng)該實(shí)現(xiàn)ICustomFormatter接口

            定義一個(gè)類

            using?System;

            namespace?MyFormat
            {
            ????
            public?class?MyBaseFormat?:?System.ICustomFormatter,?System.IFormatProvider
            ????
            {
            ????????
            //如果format?Type與當(dāng)前實(shí)例類型相同,則為當(dāng)前實(shí)例,否則為空引用
            ????????public?object?GetFormat(Type?format)
            ????????
            {
            ????????????
            if?(format?==?typeof?(ICustomFormatter))
            ????????????????
            return?this;
            ????????????
            return?null;
            ????????}


            ????????
            //實(shí)現(xiàn)Format方法說明:
            ????????
            //如果您的格式方法不支持格式,則確定正在設(shè)置格式的對象是否實(shí)現(xiàn)?IFormattable?接口。
            ????????
            //如果實(shí)現(xiàn),請調(diào)用該接口的IFormattable.ToString?方法。
            ????????
            //否則,調(diào)用基礎(chǔ)對象的默認(rèn)?Object.ToString?方法。
            ????????public?string?Format?(string?format,?object?arg,?IFormatProvider?provider)
            ????????
            {
            ????????????
            if?(format?==?null)
            ????????????
            {
            ????????????????
            if?(arg?is?IFormattable)
            ????????????????????
            return?((IFormattable)arg).ToString(format,?provider);
            ????????????????
            return?arg.ToString();
            ????????????}

            ????????????
            else
            ????????????
            {
            ????????????????
            if?(format=="MyBaseFormat")??
            ????????????????
            {
            ????????????????????
            return?"***"+arg.ToString();
            ????????????????}

            ????????????????
            else
            ????????????????
            {
            ????????????????????
            if?(arg?is?IFormattable)
            ????????????????????????
            return?((IFormattable)arg).ToString(format,?provider);
            ????????????????????
            return?arg.ToString();
            ????????????????}

            ????????????}

            ????????}

            ????}

            }

            到一控制臺中
            ????????????string?printString=String.Empty;
            ????????????
            int?i=100;
            ????????????MyBaseFormat?myBaseFormat
            =new?MyBaseFormat();

            ????????????printString
            =string.Format(myBaseFormat,"顯示正常格式:{0}",i);
            ????????????Console.WriteLine(printString);
            ????????????printString
            =string.Format(myBaseFormat,"顯示正常格式:{0:C}",i);
            ????????????Console.WriteLine(printString);
            ????????????printString
            =string.Format(myBaseFormat,"顯示自定義格式{0:MyBaseFormat}",i);
            ????????????Console.WriteLine(printString);

            ????????????Console.ReadLine();

            效果如下:


            小總結(jié):
            1.如果需要您自己的格式化包含在某個(gè)類上,在該類上實(shí)現(xiàn)IFormattable接口
            2.如果希望自定義格式化并使它可供多個(gè)不同類使用,那么實(shí)現(xiàn) ICustomFormatter接口



            希望上面提到的知識對你有所提示
            當(dāng)然歡迎交流和指正

            blog:
            http://www.cnblogs.com/aierong
            author:aierong
            email:aierong@126.com

            寫隨筆費(fèi)時(shí)又費(fèi)力,請尊重作者的權(quán)利
            謝謝!
            namespace?Microshaoft
            {
            ????
            using?System;

            ????
            public?class?ChineseFormat?:?System.ICustomFormatter,?System.IFormatProvider
            ????
            {
            ????????
            //如果format?Type與當(dāng)前實(shí)例類型相同,則為當(dāng)前實(shí)例,否則為空引用?
            ????????public?object?GetFormat(Type?format)
            ????????
            {
            ????????????
            if?(format?==?typeof?(ICustomFormatter))
            ????????????
            {
            ????????????????
            return?this;
            ????????????}

            ????????????
            return?null;
            ????????}


            ????????
            //實(shí)現(xiàn)Format方法說明:?
            ????????
            //如果您的格式方法不支持格式,則確定正在設(shè)置格式的對象是否實(shí)現(xiàn)?IFormattable?接口。?
            ????????
            //如果實(shí)現(xiàn),請調(diào)用該接口的IFormattable.ToString?方法。?
            ????????
            //否則,調(diào)用基礎(chǔ)對象的默認(rèn)?Object.ToString?方法。?
            ????????public?string?Format(string?format,?object?arg,?IFormatProvider?provider)
            ????????
            {
            ????????????
            if?(format?==?null)
            ????????????
            {
            ????????????????
            if?(arg?is?IFormattable)
            ????????????????
            {
            ????????????????????
            return?((IFormattable)?arg).ToString(format,?provider);
            ????????????????}

            ????????????????
            return?arg.ToString();
            ????????????}

            ????????????
            else
            ????????????
            {
            ????????????????
            if?(format?==?"ChineseFormat")
            ????????????????
            {
            ????????????????????
            string[]?Nums?=?new?string[]?{"",?"",?"",?"",?"",?"",?"",?"",?"",?""};
            ????????????????????
            //位?數(shù)組?
            ????????????????????string[]?Digits?=?new?string[]?{"",?"",?"",?""};
            ????????????????????
            //單位?數(shù)組?
            ????????????????????string[]?Units?=?new?string[]?{"",?"[萬]",?"[億]",?"[萬億]"};
            ????????????????????
            return?ConvertNumberToChinese(arg.ToString(),?Nums,?Digits,?Units);
            ????????????????????
            //return?"***"+arg.ToString();?
            ????????????????}

            ????????????????
            else
            ????????????????
            {
            ????????????????????
            if?(arg?is?IFormattable)
            ????????????????????
            {
            ????????????????????????
            return?((IFormattable)?arg).ToString(format,?provider);
            ????????????????????}

            ????????????????????
            return?arg.ToString();
            ????????????????}

            ????????????}

            ????????}


            ????????
            public?static?string?ConvertNumberToChinese(string?x,?string[]?Nums,?string[]?Digits,?string[]?Units)
            ????????
            {
            ????????????
            string?S?=?"";?//返回值?
            ????????????int?p?=?0;?//字符位置指針?
            ????????????int?m?=?x.Length?%?4;?//取模?

            ????????????
            //?四位一組得到組數(shù)?
            ????????????int?k?=?(m?>?0???x.Length?/?4?+?1?:?x.Length?/?4);

            ????????????
            //?外層循環(huán)在所有組中循環(huán)?
            ????????????
            //?從左到右?高位到低位?四位一組?逐組處理?
            ????????????
            //?每組最后加上一個(gè)單位:?"[萬億]","[億]","[萬]"?
            ????????????for?(int?i?=?k;?i?>?0;?i--)
            ????????????
            {
            ????????????????
            int?L?=?4;
            ????????????????
            if?(i?==?k?&&?m?!=?0)
            ????????????????
            {
            ????????????????????L?
            =?m;
            ????????????????}

            ????????????????
            //?得到一組四位數(shù)?最高位組有可能不足四位?
            ????????????????string?s?=?x.Substring(p,?L);
            ????????????????
            int?l?=?s.Length;

            ????????????????
            //?內(nèi)層循環(huán)在該組中的每一位數(shù)上循環(huán)?從左到右?高位到低位?
            ????????????????for?(int?j?=?0;?j?<?l;?j++)
            ????????????????
            {
            ????????????????????
            //處理改組中的每一位數(shù)加上所在位:?"仟","佰","拾",""(個(gè))?
            ????????????????????int?n?=?Convert.ToInt32(s.Substring(j,?1));
            ????????????????????
            if?(n?==?0)
            ????????????????????
            {
            ????????????????????????
            if?(j?<?l?-?1
            ????????????????????????????
            &&?Convert.ToInt32(s.Substring(j?+?1,?1))?>?0?//后一位(右低)?
            ????????????????????????????&&?!S.EndsWith(Nums[n]))
            ????????????????????????
            {
            ????????????????????????????S?
            +=?Nums[n];
            ????????????????????????}

            ????????????????????}

            ????????????????????
            else
            ????????????????????
            {
            ????????????????????????
            //處理?1013?一千零"十三",?1113?一千一百"一十三"?
            ????????????????????????if?(!(n?==?1?&&?(S.EndsWith(Nums[0])?|?S.Length?==?0)?&&?j?==?l?-?2))
            ????????????????????????
            {
            ????????????????????????????S?
            +=?Nums[n];
            ????????????????????????}

            ????????????????????????S?
            +=?Digits[l?-?j?-?1];
            ????????????????????}

            ????????????????}

            ????????????????p?
            +=?L;
            ????????????????
            //?每組最后加上一個(gè)單位:?[萬],[億]?等?
            ????????????????if?(i?<?k)?//不是最高位的一組?
            ????????????????{
            ????????????????????
            if?(Convert.ToInt32(s)?!=?0)
            ????????????????????
            {
            ????????????????????????
            //如果所有?4?位不全是?0?則加上單位?[萬],[億]?等?
            ????????????????????????S?+=?Units[i?-?1];
            ????????????????????}

            ????????????????}

            ????????????????
            else
            ????????????????
            {
            ????????????????????
            //處理最高位的一組,最后必須加上單位?
            ????????????????????S?+=?Units[i?-?1];
            ????????????????}

            ????????????}

            ????????????
            return?S;
            ????????}

            ????}

            }


            namespace?Test
            {
            ????
            using?System;
            ????
            using?Microshaoft;

            ????
            class?AppTest
            ????
            {
            ????????
            static?void?Main()
            ????????
            {
            ????????????
            string?printString?=?String.Empty;
            ????????????
            long?i?=?1100000013;
            ????????????ChineseFormat?fmt?
            =?new?ChineseFormat();

            ????????????printString?
            =?string.Format(fmt,?"顯示正常格式:?{0}",?i);
            ????????????Console.WriteLine(printString);
            ????????????printString?
            =?string.Format(fmt,?"顯示正常格式:?{0:C}",?i);
            ????????????Console.WriteLine(printString);
            ????????????printString?
            =?string.Format(fmt,?"顯示自定義格式:?{0:ChineseFormat}",?i);
            ????????????Console.WriteLine(printString);

            ????????????Console.ReadLine();
            ????????}

            ????}

            }

            來自:aierong原創(chuàng)技術(shù)隨筆(.Net方向應(yīng)用)

            posted on 2006-04-18 17:21 夢在天涯 閱讀(760) 評論(0)  編輯 收藏 引用 所屬分類: C#/.NET

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1807563
            • 排名 - 5

            最新評論

            閱讀排行榜

            国产福利电影一区二区三区久久老子无码午夜伦不 | 久久精品一区二区国产| www.久久99| 久久精品成人欧美大片| 久久久久久久波多野结衣高潮| 久久人人妻人人爽人人爽| AA级片免费看视频久久| 色欲综合久久躁天天躁| 性欧美丰满熟妇XXXX性久久久| 91精品国产9l久久久久| 久久久久国产日韩精品网站| 一本一道久久综合狠狠老| 久久91精品国产91久久小草| 久久久久久久97| 无码人妻久久一区二区三区蜜桃| 精品久久久久久无码专区不卡| 久久男人AV资源网站| 久久成人精品视频| 久久午夜伦鲁片免费无码| 久久免费视频6| 久久精品国产亚洲精品| 久久国产高清字幕中文| 久久66热人妻偷产精品9| 久久国产劲爆AV内射—百度| 久久亚洲国产精品123区| 久久天堂电影网| 国产精品久久久久久| 久久无码专区国产精品发布| 久久亚洲天堂| 青春久久| 少妇无套内谢久久久久| 午夜精品久久久久9999高清| 国内精品伊人久久久久影院对白| 久久久久亚洲AV无码网站| 欧美亚洲色综久久精品国产| 亚洲狠狠婷婷综合久久蜜芽| 亚洲色婷婷综合久久| 麻豆成人久久精品二区三区免费| 亚洲精品午夜国产VA久久成人| 久久亚洲sm情趣捆绑调教| 亚洲欧美日韩久久精品第一区|