2012年5月28日 星期一

利用Ascii Code建立公式

最近工作上遇到一個需求

Use希望可以看到計算出數量的公式

有個數量是要將每週的值加總起來 週數會動態的變動 我們期望公式的樣式如下:(ex:3週,要顯示A+B+C;5週,要顯示A+B+C+D+E。下面在說明變數的意義。)
A+B+C

A:第一週的值(30)
B:第二週的值(40)
C:第三週的值(40)

我們不需要將26個字母都建立 再去跑for loop取得

只要使用Ascii Code再根據我們的週數去跑for loop

.aspx



.cs
  
namespace WebTemplate
{
 public partial class FormulaExample : System.Web.UI.Page
 {
  protected void Page_Load(object sender, EventArgs e)
  {
  }
  protected void btnGenerate_Click(object sender, EventArgs e)
  {
   //若沒輸入或為空白則轉換成0
   int number =
    Convert.ToInt32(string.IsNullOrEmpty(this.txtNumner.Text.Trim()) ? "0" : this.txtNumner.Text.Trim());
   //ASCII Code 65 = "A"
   int alpha = 65;
   //結果
   string strResult = string.Empty;
   for (int i = 0; i < number; i++)
   {
    strResult += Convert.ToChar(alpha).ToString() + "+";
    alpha++;
   }
   this.lblResult.Text = strResult.TrimEnd("+".ToCharArray());
  }
  }
}



查詢Ascii Code的網站:http://www.ascii.cl/

沒有留言:

張貼留言