IFormatable


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();
        }
    }
posted @ 2008-11-07 16:37  roboth  阅读(270)  评论(0编辑  收藏  举报