代码之家  ›  专栏  ›  技术社区  ›  NetHawk

string.format“c”(currency)返回的是字符串“c”,而不是格式化的文本

  •  4
  • NetHawk  · 技术社区  · 16 年前

    我正在尝试确保从文本框派生的控件中的文本始终被格式化为货币。

    我已经像这样重写了文本属性。

       public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {                
                double tempDollarAmount = 0;
                string tempVal = value.Replace("$", "").Replace(",","");
                if (double.TryParse(tempVal, out tempDollarAmount))
                {
                    base.Text = string.Format("C", tempDollarAmount);
                }
                else
                {                 
                    base.Text = "$0.00";
                }                
            }
        }
    

    结果:

    • 如果我传递值“text” (amountcontrol.text=“text”;)的 测试页上的控件文本 如预期设置为“$0.00”。
    • 如果我通过值7 (amountcontrol.text=“7”;)i 希望看到“$7.00”,但文本 我的测试页上的控件已设置 到“C”。

    我想我错过了一些非常简单的东西。这是关于房产的事吗?或者我使用的字符串格式方法不正确?

    7 回复  |  直到 13 年前
        1
  •  13
  •   Indeed is Trash    16 年前

    而不是“c”放“0:c”

    有关更多字符串格式问题,请转到 here

        2
  •  4
  •   Joe    16 年前

    您还可以使用tempdollaramount.toString(“c”)。

        3
  •  1
  •   matt_dev    16 年前

    需要“0:c”

        4
  •  1
  •   Josh Mein    16 年前

    应该是“0:c”

        5
  •  1
  •   Gonzalo Quero    16 年前

    是的,应该是:

    base.Text = string.Format("{0:C}", tempDollarAmount);
    
        6
  •  1
  •   Stu Mackellar    16 年前

    应该是:

    base.Text = string.Format("{0:C}", tempDollarAmount);
    

    请不要用double来表示货币,而是使用十进制。

        7
  •  -1
  •   Jon Egerton Aditya kumar sahoo    13 年前

    应该是:

    Console.WriteLine("The value 99999 in various Format");
    String s = "$";
    String s1=s.Replace(s,"RS");
    String s2 = String.Format("{0:c}", s1);
    String s3="1000";
    Console.WriteLine(s2 + s3);