修改列表中的数字

时间:2013-12-20 12:33:08

标签: c# asp.net xml

我有一个返回5998 7510 8144 9458 10916 13214的变量 prix ,此列表必须更改为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 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>

4 个答案:

答案 0 :(得分:4)

由于代码示例与您作为示例提供的列表无关,因此不太清楚您的要求。如果你需要从末尾添加一个逗号两个字母,试试这个,

string val = "7958";
string newVal = val.Insert(val.Length-2,",");
Console.WriteLine(newVal);

答案 1 :(得分:2)

请尝试以下操作:

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

答案 2 :(得分:1)

您可以在页面中使用DataBound事件作为示例:

<asp:Repeater ID="rpMyRepeater1" ItemType="" runat="server" OnItemDataBound="rpMyRepeater1_ItemDataBound">
    <ItemTemplate>
        <asp:Literal ID="ltl" runat="server"></asp:Literal>
    </ItemTemplate>
</asp:Repeater>

在你的代码behine中:

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

答案 3 :(得分:1)

怎么样:

        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