【问题标题】:Trying to add the currency symbol to value using system.globalization尝试使用 system.globalization 将货币符号添加到值
【发布时间】:2011-07-26 10:26:27
【问题描述】:

我正在尝试使用以下代码将货币符号添加到总价值

using System.Globalization;

double value;
double totalValue = 0.0;            

foreach (DataRow row in reportData.Rows)
{
    if (double.TryParse(row["value"].ToString(), out value))
    {
        totalValue += value;
        RegionInfo rgi = new RegionInfo("en-UK");
        totalValue += Convert.ToString(string.Format("   CurrencySymbol:               {0}\n", rgi.CurrencySymbol));             
     }
 }

它给出了错误 can't convert type string to double 有人能帮我吗

修改后的代码:

       int count;
      int total = 0;
      double value;

      double totalValue = 0.0;




      foreach (DataRow row in reportData.Rows)
      {
        if (double.TryParse(row["value"].ToString(), out value))
        {
          totalValue += value;


        }

        if (!Overview && int.TryParse(row["mshipssold"].ToString(), out count))
        {
          total += count;
        }
      }
      DataRow totalRow = reportData.NewRow();
      totalRow["mshipType_Name"] = "Total";
      totalRow["mshipssold"] = total;
      totalRow["value"] = totalValue;

      reportData.Rows.Add(totalRow);

      targetChartControl.Series["Value"].Points.DataBindXY(reportData.Rows, "mshipType_Name", reportData.Rows, "value");

      if (!Overview)
      {
        targetChartControl.Series["Quantity"].Points.DataBindXY(reportData.Rows, "mshipType_Name", reportData.Rows, "mshipssold");
      }
    }

修改代码:1

我想在蓝条上显示 3830 英镑、2070 英镑、5090 英镑

这是我的代码

   try
    {
    DataTable reportData = KPIData.MembershipSales(StartDate, EndDate, mf);
    Series quantitySeries;
    Series valueSeries = null;
    Title title;
    string area;

    targetChartControl.ChartAreas.Clear();
    targetChartControl.Series.Clear();
    targetChartControl.Titles.Clear();

    area = "Value";
    targetChartControl.ChartAreas.Add(area);
    quantitySeries = targetChartControl.Series.Add(area);
    quantitySeries.ChartArea = area;

    if (!Overview)
    {
      title = targetChartControl.Titles.Add("Membership Sales by Total Contract Value by Type");
      title.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
      title.Alignment = ContentAlignment.TopLeft;
      title.DockedToChartArea = area;

      targetChartControl.Titles.Add("").DockedToChartArea = area;
    }

    targetChartControl.Titles.Add("Membership sale values").DockedToChartArea = area;


    if (!Overview)
    {
      area = "Quantity";
      targetChartControl.ChartAreas.Add(area);
      quantitySeries = targetChartControl.Series.Add(area);
      quantitySeries.ChartArea = area;

      title = targetChartControl.Titles.Add("Membership Sales by Quantity");
      title.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
      title.Alignment = ContentAlignment.TopLeft;
      title.DockedToChartArea = area;

      targetChartControl.Titles.Add("").DockedToChartArea = area;
      targetChartControl.Titles.Add("Membership sale quantities").DockedToChartArea = area;
    }

    foreach (Title chartTitle in targetChartControl.Titles)
    {
      chartTitle.IsDockedInsideChartArea = false;
    }

    foreach (ChartArea chartArea in targetChartControl.ChartAreas)
    {
      chartArea.Area3DStyle.Enable3D = true;
      chartArea.AxisX.LabelStyle.IsEndLabelVisible = true;
    }

    foreach (Series series in targetChartControl.Series)
    {
      series.ChartType = SeriesChartType.StackedColumn;
      series["ColumnDrawingStyle"] = "SoftEdge";
      series["LabelStyle"] = "Top";

      series.IsValueShownAsLabel = true;
      //          series.CustomProperties = "DrawingStyle=Cylinder";
      series.BackGradientStyle = GradientStyle.DiagonalLeft;
    }

    foreach (Legend legend in targetChartControl.Legends)
    {
      legend.Enabled = false;
    }

    if (reportData == null)
    {
      valueSeries.Points.Clear();
      valueSeries.Points.AddXY("No sales for this time period", 0);

      if (!Overview)
      {
        quantitySeries.Points.Clear();
        quantitySeries.Points.AddXY("No sales for this time period", 0);
      }
    }
    else
    {
      int count;
      int total = 0;
      double value;

      double totalValue = 0.0;




      foreach (DataRow row in reportData.Rows)
      {
        if (double.TryParse(row["value"].ToString(), out value))
        {
          totalValue += value;    

        }

        if (!Overview && int.TryParse(row["mshipssold"].ToString(), out count))
        {
          total += count;
        }
      }

      CultureInfo rgi = new CultureInfo("en-GB");
      string totalcurrency = string.Format(rgi, "{0:C}", totalValue);

      DataRow totalRow = reportData.NewRow();
      totalRow["mshipType_Name"] = "Total";
      totalRow["mshipssold"] = total;
      totalRow["value"] = totalcurrency;

      reportData.Rows.Add(totalRow);

      targetChartControl.Series["Value"].Points.DataBindXY(reportData.Rows, "mshipType_Name", reportData.Rows, "value");

      if (!Overview)
      {
        targetChartControl.Series["Quantity"].Points.DataBindXY(reportData.Rows, "mshipType_Name", reportData.Rows, "mshipssold");
      }
    }
  }
  catch
  {
  }

【问题讨论】:

  • 我相信您正在显示图表并希望符号出现在图表上?如果是这样,您应该尝试在图表控件中设置格式,例如 Y 轴的 Format='C'

标签: c# .net double globalization mschart


【解决方案1】:

您需要进行总计,然后将其转换为字符串。

double totalValue;
foreach (DataRow row in reportData.Rows) {
  double value;
  if (double.TryParse(row["value"].ToString(),out value) totalValue+= value;
}
CultureInfo rgi = new CultureInfo("en-GB");
string totalValueCurrency=string.Format(rgi,"{0:c}", totalValue);

回答您修改后的问题:

DataRow totalRow = reportData.NewRow();之前插入两行

CultureInfo rgi = new CultureInfo("en-GB");
string totalValueCurrency=string.Format(rgi,"{0:c}", totalValue);

然后改变

totalRow["value"] = totalValue;

阅读

totalRow["value"] = totalValueCurrency;

但是,如果您的列的数据类型不是字符串,这可能会出现严重错误。您最好更改报告以进行格式化。

如果您使用的是 Microsoft 图表控件,您可能需要这个:

修改这段代码

foreach (ChartArea chartArea in targetChartControl.ChartAreas)    {
  chartArea.Area3DStyle.Enable3D = true;
  chartArea.AxisX.LabelStyle.IsEndLabelVisible = true;
  // New Line Here
  chartArea.AxisY.LabelStyle.Format = "C";
}

【讨论】:

  • 将符号添加到总值的位置
  • 我没有把它放到totalValueCurrency中,双打不能保存货币符号,只有字符串可以,所以我运行你的求和算法,然后将结果保存到字符串变量中
  • 你能看看我修改后的代码,我必须在其中添加符号,我需要在报告中显示符号......我有点困惑......请帮助我.....
  • @ Bob vale 好的,我对此感到困惑,总价值和总价值货币之间没有关系。这两个是相同还是不同,我想将货币符号添加到价值和总价值是否可能
  • @user682417 - DOH!我没有正确阅读您的格式字符串,并假设您自己在哪里格式化 totalValue。我现在已经更正了答案
【解决方案2】:

您不能将字符串存储在 double 中 - 当 totalValue 为 double 时,这将永远无法工作。

totalValue += Convert.ToString(string.Format(" CurrencySymbol: {0}\n", rgi.CurrencySymbol));

【讨论】:

    【解决方案3】:

    这行有点混乱:

              totalValue += Convert.ToString(string.Format("   CurrencySymbol:               {0}\n", rgi.CurrencySymbol));
    

    totalValue 是一个双精度数,您正在尝试向它添加一个字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多