I wrote a program that converts Celsius to Fahrenheit and vice versa. when converting Celsius to Fahrenheit I used the formula (9/5) * Celsius + 32. but that didn't work, I discovered that 9/5 was returning 1 rather than 1.8 and I'm not sure why that is. I fixed the problem by just using the formula: 1.8 * Celsius + 32, but I don't understand why I had to. The Fahrenheit to Celsius bit works fine.

My code :

double dubCelsius;
double dubFarenheit;

if (rbtnFar.Checked)
{
double.TryParse(txtInput.Text, out dubFarenheit);
dubCelsius = (dubFarenheit - 32) * 5 / 9;
txtResult.Text = dubCelsius.ToString();
}

if (rbtnCel.Checked)
{
double.TryParse(txtInput.Text, out dubCelsius);
dubFarenheit = 1.8 * dubCelsius + 32;
txtResult.Text = dubFarenheit.ToString();
}

I have a feeling the answer to this is going to be very obvious but I just cant figure it out.

Both the 5 and 9 literals are integer, therefore the result will be an integer (not rounded; the decimal value is simply removed). You can explicitly declare them as double values by entering then as 5.0 and 9.0 respectively.

You may want to have a look at "Section 7.3 Operators" of the C# Language Specification.

that did it. thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.