尝试使用system.globalization将货币符号添加到值

时间:2011-07-26 10:26:27

标签: c# .net double globalization mschart

我正在尝试使用以下代码

将货币符号添加到totalvalue
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));             
     }
 }

它提供错误无法将类型string转换为double 请问任何人帮助我

修改后的CODE:

       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

图表enter image description here

我希望在蓝条上显示这样的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
  {
  }

3 个答案:

答案 0 :(得分:4)

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

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 Chart Control,则可能需要此代码:

修改此部分代码

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

答案 1 :(得分:0)

您不能将字符串存储在double中 - 当totalValue为double时,这将永远不会有效。

  

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

答案 2 :(得分:0)

这条线有点混乱:

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

totalValue是双倍的,您正在尝试为其添加字符串。