2010-11-08 69 views
0

我有一個問題做出釐米到C#腳/英寸的轉換器,這是一個多麼GOT:釐米至英寸轉換器,兩個文本框multply值

<asp:textbox id="txtFoot" runat="server"></asp:textbox> 

<asp:textbox id="txtInches" runat="server"></asp:textbox> 

<asp:Button id="btnAdd" runat="server" text="Count" onclick="btnAdd_Click" /> 

<br /> 

<asp:Label ID="lblResult" runat="server"></asp:Label>is<asp:Label ID="lblFootAndInches" runat="server"></asp:Label>cm  
<%--I try to get a result "10'1" is 3,939 cm"--%> 

protected void btnAdd_Click(object sender, EventArgs e) 
{ 
    lblResult = (txtFoot.Text + "," + txtInches.Text) * 0,39; //I would like to get 10,1 * 0,39 = 3,939 (10 foot and 1 inch) 
    lblFootAndInches = txtFoot.Text + "'" + txtInches.Text + '"'; //I'm looking for a result like 10'1" 
} 

回答

2

你的代碼有幾個錯誤。你會需要太多關於消除全球化和區域選項的問題(系統是否使用作爲一個小數點的性格,我想提出代碼更改爲以下:

string separator = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator; 
txtFoot.Text = txtFoot.Text.Replace(".", separator).Replace(",", separator); 
txtInches.Text = txtInches.Text.Replace(".", separator).Replace(",", separator); 

Double result = (Double.Parse(txtFoot.Text) * 30.48) + (Double.Parse(txtInches.Text) * 2.54); 
lblResult.Text = result.ToString(); 
lblFootAndInches.Text = string.Format("{0}'{1}\"", txtFoot.Text, txtInches.Text); 

如果你不必擔心區域設置,跳過第一個三行代碼

希望這有助於

+0

我在Replace上遇到錯誤,無法解析符號Replace和lblResult,無法轉換源類型爲目標類型的字符串,但thx試圖幫助 – Nicklas 2010-11-09 06:22:37

+0

對不起,顯然,如果你看看我的代碼,你會發現我省略了一些.Text屬性,現在已經修復了 – 2010-11-09 14:11:54

+1

很棒完美的作品!非常感謝! – Nicklas 2010-11-09 16:18:17

3

我不知道ASP.NET,但我認爲我可以處理此代碼...

protected void btnAdd_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     lblResult.Text = (Double.Parse(txtFoot.Text + "," + txtInches.Text) * 0.39).ToString(); 
     lblFootAndInches.Text = txtFoot.Text + "'" + txtInches.Text + "\""; 
    } 
    catch (FormatException s) 
    { 
     //Do some exception handling here, like warning the viewer to enter a valid number. 
    } 
} 

我希望這有助於!

+0

我得到一個錯誤:。無法將源類型doubel到目標字符串類型 – Nicklas 2010-11-08 20:49:29

+0

那一定是因爲你進入值在任何TextBox中不能在double中轉換,或者不是有效的雙字符串。嘗試使用'double.TryParse()'來代替,以便您可以充分處理'FormatException'。 – 2010-11-08 20:58:19

+0

爲此,您最好使用'NumericUpDown'或其他等效的ASP.NET。我不知道是否有什麼。 – Vercas 2010-11-08 21:26:26

0

我建議如下:

protected void btnAdd_Click(object sender, EventArgs e) { 
    int feet = 0; 
    int inches = 0; 

    if (!int.TryParse(txtFoot.Text, out feet)) 
     throw new FormatException(string.Format("{0} is not a valid value", txtFoot.Text)); 

    if (!int.TryParse(txtInches.Text, out inches)) 
     throw new FormatException(string.Format("{0} is not a valid value", txtInches.Text)); 

    double meters = ((double)(string.Format("{0}.{1}", feet, inches)) * .39; 
    lblResult.Text = string.Format("{0}", meters); 
    lblFootAndInches.Text = string.Format("{0}'{1}\"", feet, inches); 
} 

算法

  1. 你驗證用戶輸入的INT解析的字符串到每個TextBox ES的;
  2. 建立你的代表盎格魯 - 撒克遜測量的字符串,然後將其乘以得到它的轉換(就像你在樣本中做的那樣);
  3. 將結果顯示在您的Result Label;
  4. 以盎格魯 - 撒克遜測量單位格式輸出結果以適合TextBox

Disclaimer

This code was not compiled but written right off the top of my head. So minor changes might be necessary in order to compile correctly and have the correct expected behaviour.

這有幫助嗎?

+0

看起來不錯,我得到的唯一錯誤是「((double)(string。」):無法將類型爲string的表達式類型轉換爲double。 – Nicklas 2010-11-09 06:25:52

相關問題