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

发布于 2024-11-26 05:19:09 字数 5374 浏览 2 评论 0原文

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

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 有人可以帮我

修改代码:

       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

graph在此处输入图像描述

我想这样显示 £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
  {
  }

i am trying to add the currency symbol to totalvalue by using following code

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));             
     }
 }

it was giving error can not convert type string to double
would any one pls help me

Modified 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");
      }
    }

Modified code :1

graphenter image description here

I want to show like this £3830 , £2070 , £5090 on the blue bars

and this is my code

   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
  {
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

翻身的咸鱼 2024-12-03 05:19:09

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

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";
}

You need to do your totalling and then convert it to a string.

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);

In answer to your modified question:

before before DataRow totalRow = reportData.NewRow(); insert the two lines

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

and then change

totalRow["value"] = totalValue;

to read

totalRow["value"] = totalValueCurrency;

However this might go horribly wrong if the datatype of your column is not string. You may be better of changing your report to do the formatting for you.

If you are using Microsoft Chart Control you may want this instead:

Modify this section of code

foreach (ChartArea chartArea in targetChartControl.ChartAreas)    {
  chartArea.Area3DStyle.Enable3D = true;
  chartArea.AxisX.LabelStyle.IsEndLabelVisible = true;
  // New Line Here
  chartArea.AxisY.LabelStyle.Format = "C";
}
鸩远一方 2024-12-03 05:19:09

您不能将字符串存储在双精度型中 - 当 TotalValue 为双精度型时,这将永远不起作用。

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

You cannot store a string in a double - this will never work when totalValue is a double.

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

胡大本事 2024-12-03 05:19:09

这行代码有点混乱:

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

totalValue 是一个双精度值,而您正尝试向其中添加一个字符串。

This line is a bit confused:

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

totalValue is a double and you're trying to add a string to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文