Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# string.Format";";(currency)正在返回字符串";";而不是格式化文本_C#_Format - Fatal编程技术网

C# string.Format";";(currency)正在返回字符串";";而不是格式化文本

C# string.Format";";(currency)正在返回字符串";";而不是格式化文本,c#,format,C#,Format,我试图确保控件中从TextBox派生的文本始终格式化为货币 我已经像这样重写了Text属性 public override string Text { get { return base.Text; } set { double tempDollarAmount = 0; string tempVal

我试图确保控件中从TextBox派生的文本始终格式化为货币

我已经像这样重写了Text属性

   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”
我想我遗漏了一些非常简单的东西。是关于房产的事吗?还是我使用的字符串格式方法不正确?

而不是“C”放“{0:C}”


要了解更多字符串格式问题,go

需要是“{0:C}”

您也可以使用tempDollarAmount.ToString(“C”)。

它应该是“{0:C}”

是的,它应该是:

base.Text = string.Format("{0:C}", tempDollarAmount);
base.Text = string.Format("{0:C}", tempDollarAmount);
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);
应该是:

base.Text = string.Format("{0:C}", tempDollarAmount);
base.Text = string.Format("{0:C}", tempDollarAmount);
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);
请不要用double表示货币,用decimal代替。

应该是:

base.Text = string.Format("{0:C}", tempDollarAmount);
base.Text = string.Format("{0:C}", tempDollarAmount);
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);

谢谢我把这个作为答案,因为你是第一个。谢谢你的链接。谢谢,乔。我很高兴看到“C”格式字符串是如何实际使用的。我个人认为这是一个更好的方法,请确认哪个更有效?嘿,不要这样做。它稀释了真正的答案。