开发者

cm to inch converter, two textboxes multply a value

I Have a problem to make a cm to foot/inch converter in C#, this is what a got:

<asp:textbox id="txtFoot" runat="server"></asp:textbox>开发者_C百科;

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


There are several errors in your code. You'll need too eliminate the issue regarding globalization and regional options (whether the system uses , or . as a decimal point character, I'd propose changing the code to the following:

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

If you do not need to worry about regional settings, skip first three lines of code.

Hope this helps.


I know no ASP.NET but I think I can handle this code...

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

I hope this helps!


I would suggest the following:

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

Algorithm

  1. You validate that the user entered a int parseable string into each of the TextBoxes;
  2. Build up your string representing the anglo-saxon measurement, then multiply it to get it converted (as you have done in your sample);
  3. Display the result in your Result Label;
  4. Format your result in anglo-saxon unit of measurement for output in the appropriate 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.

Does this help?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜