C# 如何 实现 像VB 的 Format函数 一样的功能 ??
如何实现 vb 下面的功能 :
 Format(12, "'D-'#")
为 D-12 Format(12, "#.00")
为 12.00 Dim d As Date
  d = "06-2-6"
  Label2.Text = Format(d, "yyyy-MM-dd")
为 2006-02-06
  Dim d As DateTime
 d = "06-2-3 12:25:23"
'大写H为24小时制
Label2.Text = Format(d, "hh点mm分ss秒")
为12点25分23秒
C# 如何实现??

解决方案 »

  1.   

    自带有函数的
    例:
    DateTime d = Convert.ToDateTime("2006-2-6");
    Label2.Text = d.ToString("yyyy-MM-dd");为 2006-02-06
      

  2.   

    反编译VB的Formatpublic static string Format(object Expression, [Optional] string Style /* = "" */)
    {
          string text1;
          try
          {
                double num1;
                float single1;
                IFormatProvider provider1 = null;
                IFormattable formattable1 = null;
                if ((Expression == null) || (Expression.GetType() == null))
                {
                      return "";
                }
                if ((Style == null) || (Style.Length == 0))
                {
                      return Conversions.ToString(Expression);
                }
                IConvertible convertible1 = (IConvertible) Expression;
                TypeCode code1 = convertible1.GetTypeCode();
                if (Style.Length > 0)
                {
                      try
                      {
                            string text2 = null;
                            if (Strings.FormatNamed(Expression, Style, ref text2))
                            {
                                  return text2;
                            }
                      }
                      catch (StackOverflowException exception1)
                      {
                            throw exception1;
                      }
                      catch (OutOfMemoryException exception2)
                      {
                            throw exception2;
                      }
                      catch (ThreadAbortException exception3)
                      {
                            throw exception3;
                      }
                      catch (Exception)
                      {
                            return Conversions.ToString(Expression);
                      }
                }
                formattable1 = Expression as IFormattable;
                if (formattable1 == null)
                {
                      code1 = Convert.GetTypeCode(Expression);
                      if ((code1 != TypeCode.String) && (code1 != TypeCode.Boolean))
                      {
                            throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", new string[] { "Expression" }));
                      }
                }
                switch (code1)
                {
                      case TypeCode.Empty:
                            return "";                  case TypeCode.Object:
                      case TypeCode.Char:
                      case TypeCode.SByte:
                      case TypeCode.Byte:
                      case TypeCode.Int16:
                      case TypeCode.UInt16:
                      case TypeCode.Int32:
                      case TypeCode.UInt32:
                      case TypeCode.Int64:
                      case TypeCode.UInt64:
                      case TypeCode.Decimal:
                      case TypeCode.DateTime:
                            return formattable1.ToString(Style, provider1);                  case TypeCode.DBNull:
                            return "";                  case TypeCode.Boolean:
                            return string.Format(provider1, Style, new object[] { Conversions.ToString(convertible1.ToBoolean(null)) });                  case TypeCode.Single:
                            single1 = convertible1.ToSingle(null);
                            if ((Style == null) || (Style.Length == 0))
                            {
                                  return Conversions.ToString(single1);
                            }
                            goto Label_01C3;                  case TypeCode.Double:
                            num1 = convertible1.ToDouble(null);
                            if ((Style == null) || (Style.Length == 0))
                            {
                                  return Conversions.ToString(num1);
                            }
                            break;                  case (TypeCode.DateTime | TypeCode.Object):
                            goto Label_01F8;                  case TypeCode.String:
                            return string.Format(provider1, Style, new object[] { Expression });                  default:
                            goto Label_01F8;
                }
                if (num1 == 0)
                {
                      num1 = 0;
                }
                return num1.ToString(Style, provider1);
          Label_01C3:
                if (single1 == 0f)
                {
                      single1 = 0f;
                }
                return single1.ToString(Style, provider1);
          Label_01F8:
                text1 = formattable1.ToString(Style, provider1);
          }
          catch (Exception exception4)
          {
                throw exception4;
          }
          return text1;
    }
      

  3.   

    Format(12, "'D-'#")
    为  Format(12, "#.00")
    为 12.00
    string.format("D-{0}",12) //为D-12string.format("{0}.00",12)//为 12.00
      

  4.   

    textBox1.Text =  string.Format ("{0}.00",2.2);结果是  2.2.00为什么不是 2.20 ??