现在的位置: 首页 > 综合 > 正文

IFormatable

2013年02月05日 ⁄ 综合 ⁄ 共 1568字 ⁄ 字号 评论关闭

namespace RobotH.MyFormat
{
    
using System;
    
using System.Globalization;
    
class MyClass : System.IFormattable
    {
        
private double _d;
        
public MyClass(double d)
        {
            
this._d = d;
        }

        #region IFormattable 成员

        public string ToString(string format, IFormatProvider formatProvider)
        {
            
if (formatProvider != null)
            {
                ICustomFormatter cust 
= formatProvider.GetFormat(this.GetType()) as ICustomFormatter;
                
if (cust != null)
                {
                    
return cust.Format(format, this, formatProvider);
                }
                
else
                {
                    
return _d.ToString(format, formatProvider);
                }
            }
            
else
            {
                
switch (format)
                {
                    
case "special":
                        
return "special";
                    
default:
                        
return _d.ToString(format, formatProvider);
                }
            }
        }

        #endregion
    }
    
class App
    {
        
static void Main()
        {
            CultureInfo ci 
= null;
            MyClass cls 
= new MyClass(4);        
            Console.WriteLine(
"China Currency's Format"+cls.ToString("C",null));
            ci
=CultureInfo.CurrentCulture;
            Console.WriteLine(
"system Currency's Format"+cls.ToString("C",ci));
            ci
=new CultureInfo("zh-HK");
            Console.WriteLine(
"system Currency's Format" + cls.ToString("C", ci));
            Console.Read();
        }
    }

抱歉!评论已关闭.