C# 修改列表中的数字
我有一个变量prix,返回5998 7510 8144 9458 10916 13214,此列表必须更改为59,98 75,10 81,44 94,58 109,16 132,14C# 修改列表中的数字,c#,asp.net,xml,C#,Asp.net,Xml,我有一个变量prix,返回5998 7510 8144 9458 10916 13214,此列表必须更改为59,98 75,10 81,44 94,58 109,16 132,14 我该怎么做呢。也就是说,在每个数字右侧的最后两位数字之前加一个 这是我迄今为止尝试过的C代码: XDocument docxx = XDocument.Parse(Session["xmlrs" + z + ""].ToString()); //This now holds the set of a
我该怎么做呢。也就是说,在每个数字右侧的最后两位数字之前加一个 这是我迄今为止尝试过的C代码:
XDocument docxx = XDocument.Parse(Session["xmlrs" + z + ""].ToString());
//This now holds the set of all elements named field
try
{
XNamespace foobar = "http://www.april-technologies.com";
var prix = docxx.Descendants(foobar + "CotisationMensuelle").Select(x => new { CotisationMensuelle =(string)x}).ToList();
rpMyRepeater1.DataSource = prix;
rpMyRepeater1.DataBind();
}
catch(Exception ex) {}
C代码是:
<asp:Repeater ID="rpMyRepeater1" ItemType="" runat="server">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "CotisationMensuelle").ToString() %>
<br />
</ItemTemplate>
</asp:Repeater>
不太清楚您在编写示例代码时提出的问题与您作为示例给出的列表无关。如果需要在末尾添加两个字母的逗号,请尝试以下操作:
string val = "7958";
string newVal = val.Insert(val.Length-2,",");
Console.WriteLine(newVal);
对于示例,您可以在页面中使用数据绑定事件:
<asp:Repeater ID="rpMyRepeater1" ItemType="" runat="server" OnItemDataBound="rpMyRepeater1_ItemDataBound">
<ItemTemplate>
<asp:Literal ID="ltl" runat="server"></asp:Literal>
</ItemTemplate>
</asp:Repeater>
在您的代码中:
int i = 0;
protected void rpMyRepeater1_ItemDataBound(object Sender, RepeaterItemEventArgs e)
{
// if the item on repeater is a item of data
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// get value
string value = DataBinder.Eval(e.Item.DataItem, "CotisationMensuelle").ToString();
// find literal
Literal ltl = (Literal)e.Item.FindControl("ltl");
// check if i < 2
if (i < 2)
{
// write value
ltl.Text = value;
//increase i
i++;
}
else
{
// write value and <br/>
ltl.Text = string.Format("{0}<br />", value);
// zero i
i = 0;
}
}
}
那么:
string data = "5998 7510 8144 9458 10916 13214";
string newValue = string.Join(" ",
from s in data.Split(' ')
select (double.Parse(s) / 100).ToString("0.00"));
//59,98 75,10 81,44 94,58 109,16 132,14
请尝试以下操作:
XNamespace foobar = "http://www.april-technologies.com";
var prix = docxx.Descendants(foobar + "CotisationMensuelle").Select(x => new { CotisationMensuelle =(string)x}).ToList();
var newPrix = new List<string>();
foreach (var s in prix)
{
var s1 = s.CotisationMensuelle.Substring(0, s.CotisationMensuelle.Length - 2);
var s2 = s.CotisationMensuelle.Substring(s.CotisationMensuelle.Length - 2);
//add a comma before the last 2 digits (from right)
var s_total = s1 +","+ s2;
newPrix.Add(s_total);
}
rpMyRepeater1.DataSource = newPrix.Select(x => new {CotisationMensuelle = x}).ToList();
rpMyRepeater1.DataBind();
我收到错误消息:“AnonymousType1”不包含“Length”的定义,并且找不到接受“AnonymousType1”类型的第一个参数的扩展方法“Length”?是否缺少using指令或程序集引用?我已添加此变量s_total=s1+,+s2;现在我得到59,9875,1081,4494,58109,16132,14谢谢:在右边最后两位之前