# Thread: float/int issue if converting C to F

1. ## float/int issue if converting C to F

Hello,

I created a program to convert Celsius to Fahrenheit.

Code:
```#include <iostream>
using namespace std;
float Fahrenheit, Celsius;
int main()
{
float Fahrenheit;
float Celsius;

cout<<"Enter the temperature in Fahrenheit: ";
cin>>Fahrenheit;

Celsius=(Fahrenheit-32)*5/9;

cout<<"The temperature in Celsius is: "<<Celsius;
cout << (int)Celsius << endl;
system ("pause");

return 0;
}```
But it needs to round up or down. Meaning 55 F need to be 13 Celsius and not 12.7... I read now that I need to change it to int..which I did right here

Code:
```cout<<"The temperature in Celsius is: "<<Celsius;
cout << (int)Celsius << endl;
system ("pause");```
Also I need to do it by adding 0.5 to the calculation expression. Well that's it

Code:
`Celsius=(Fahrenheit-32)*5/9;`
but of course it wont work, if I do add + 0.5 to it. Can someone help me with this?

2. Casting always rounds down.

Code:
`cout << (int) (Celsius + 0.5) << endl;`

3. You could find the the value of the first decimal place by multiplying by ten, and finding the remainder when you divide by ten:

Code:
`int d = (Celsius * 10) % 10;`
If that number is greater than 5, cast it to an int and add one. If not, just cast it to an int.

4. Originally Posted by cyberfish
Casting always rounds down.

Code:
`cout << (int) (Celsius + 0.5) << endl;`

Dosen't work... 55F is still 12.7 C if I do this.

5. Isn't this C++?

Dosen't work... 55F is still 12.7 C if I do this.
I do not beleive that you can output integer as 12.7

6. Originally Posted by vart
Isn't this C++?
Certainly looks like it, so this thread has been moved to the C++ programming forum.

7. The reason it is not working is simple....
casting does not "round" either way.
casting a decimal to an int simply truncates everything after the '.' leaving only the int value in the final int variable

you would have to add 0.5 post F-C conversion and pre cast to int...
Code:
```Celsius=((Fahrenheit-32)*5/9)+0.5;
cout << (int)Celsius << endl;```
...wow, edited for multiple typos lol

8. Why do you declare your variable globe and then local???

Code:
```float Fahrenheit, Celsius;

int main()
{

float Fahrenheit;
float Celsius;```

jw....

9. Hey!

Thanks for the input. It helped a lot! Basically the floating-point datay type is used for those things?

Like this following code

Code:
```#include <iostream>
using namespace std;

// Function Prototypes

void pause(void);

// Variables

float      pennies;
float      nickels;
float      dimes;
float      quarters;
double       total_value;

//******************************************************
// main
//******************************************************

int main(void)
{
// Input
cout << "\nHow many pennies do you have? --->: ";
cin >> pennies;
cout << "\nHow many nickles do you have? -->: ";
cin >> nickels;
cout << "\nHow many dimes do you have -->: ";
cin >> dimes;
cout << "\nHow many quarters do you have -->: ";
cin >> quarters;

// Process
total_value = (pennies * 0.01) + (nickels * 0.05) + (dimes * 0.1) + (quarters * 0.25) ;

// Output
cout << "\nThe total value of your coins is: ";
cout << total_value;

pause();
return 0;
}

//******************************************************
// pause
//******************************************************

void pause(void)
{
cout << "\n\n";
system("PAUSE");
cout << "\n\n";
return;
}```
Each coins value is a floating-point data type, a fraction of a dollar. So I use the floating-point like this, right? But int would have done it too in this case. So why use floating-point? Cause of the larger values I might get?

10. A good way to do this is:

Code:
```#include <iostream>
#include<cmath>
using namespace std;
float Fahrenheit, Celsius;
int main()
{
float Fahrenheit;
float Celsius;

cout<<"Enter the temperature in Fahrenheit: ";
cin>>Fahrenheit;

Celsius=(Fahrenheit-32)*5/9;

float decimal = Celsius - floorf(Celsius);
if(decimal < 0.5)
{
Celsius = floorf(Celsius);
}
else
{
Celsius = ceilf(Celsius);
}

cout << "The temperature in Celsius is: " << Celsius << endl;
system ("pause");

return 0;
}```
the new code is in bold

and for the last question, an int would have done it since you are using floating points with coin values...

11. When writing code that deals with fractional values, get in the habit of appending ".0" to all integer values to force the compiler to generate floating point code.

Instead of x*5/9, write x*5.0/9.0, and the math will automatically happen in the floating point domain.

As far as rounding, the trick:

Code:
`int rounded = (int)(val + 0.5);`
Only works if val is positive. If val is negative this will round the wrong way. The correct strategy is:

Code:
`int rounded = (int)(val + (val >= 0) ? 0.5 : -0.5)`

12. Code:
`int rounded = (int)(val + (val >= 0) ? 0.5 : -0.5)`
Code:
`int rounded = (int)(val + ((val >= 0) ? 0.5 : -0.5));`
Soma

13. Forgive me but the only difference I see is the parens. Addition is commutative, though.

14. Forgive me but the only difference I see is the parens.
Well, that's pretty much the only difference. ^_^

Addition is commutative, though.
This is an issue of precedence; not an issue of associativity or mathematics.

Soma

Popular pages Recent additions