-1

I have to write a simple Fahrenheit to Celsius converter using WinForms, but the converted (Celsius) temperature does not display as expected after clicking the button. It always shows a Celsius temp of 0. The program has a simple textbox for entering Fahrenheit temperature, a convert button, and a label to output the converted temperature.

I've tried hard coding the Celsius value, as well as using several different Fahrenheit values including obvious values like 212 degrees F.

namespace FtoCConverter
{
    public partial class TempConverter : Form
    {
        public decimal fahrenheit;
        public decimal celsius = 50;
        public TempConverter()
        {
            InitializeComponent();
        }

        private void convertButton_Click(object sender, EventArgs e)
        {
            fahrenheit = decimal.Parse(fTempEntry.Text);
            celsius = (5 / 9) * (fahrenheit - 32);

            CelsiusLabel.Text = string.Format($"Your temperature is {0:0.00} degrees Celsius.", celsius);
            fTempEntry.Focus();
        }
    }
}

The correct temp would be displayed in degrees Celsius.

Peter Smith
  • 5,528
  • 8
  • 51
  • 77

1 Answers1

0

As you are using string interpolation, try

celsius = (5m / 9) * (fahrenheit - 32);
CelsiusLabel.Text = $"Your temperature is {celsius:0.00} degrees Celsius.";
Peter Smith
  • 5,528
  • 8
  • 51
  • 77