2013-12-20 16 views
0

修改的數字我有一個變量大獎賽返回5998 7510 8144 9458 10916 13214和這個名單必須改變59,98 75,10 81,44 94,58 109,16 132,14 我怎樣才能做到這一點。也就是說,最後的2個位數(右一)每個數字的前面加上,在列表

這是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> 
+0

在這一點在此代碼是你真正嘗試添加的號碼之間的逗號? –

+0

最後兩位數字(右起) – user3030806

回答

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

我得到erreur消息:''AnonymousType#1'沒有包含'Length'的定義,也沒有接受'AnonymousType#1'類型的第一個參數的擴展方法'Length'可以找到(是否缺少using指令或程序集引用?)' – user3030806

+1

@ user3030806更新代碼嘗試一下 – har07

+0

我已經加入此'VAR s_total = S1 +「」 + S2;'現在我得到** 59 ,98個75,10 81,44 94,58 109,16 132,14 **謝謝:) – user3030806

4

不是很清楚你的要求,你的代碼示例無關列表中你給作爲eample。如果您需要從末尾加上一個逗號兩個字母,試試這個,

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

你可以使用DataBound事件,樣品,在你的頁面:在你的代碼behine

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