-3

I want to make a program that converts Fahrenheit to Kelvins but in the conversion, you need to add 273.15. My current code for the conversion is


    int finalTemp = (temp1 - 32) * 5 / 9 + 273.15;

but the compiler gives back:

Error CS0266 Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)

How do I make it recognize 273.15 as a decimal?

DRESphAl
  • 11
  • 1
  • 5
  • An `int` is an integer type. It holds whole numbers, with no decimal point. Given that description, does it look like `int` is the proper type to hold anything multipled or divided by `273.15`? – Ken White Jan 12 '19 at 01:56
  • 2
    @IshaanJavali: It's unbecoming to beg for an accept. You should also be aware that there is a time limit (waiting period) before a new user can accept an answer. Give the person time to at least figure out if your answer works or if someone else posts a better one before trying to force them to accept yours. You haven't even allowed 15 minutes before imploring for a vote, and that's rude. – Ken White Jan 12 '19 at 02:07
  • Thank you for that Ken. I was just asking if he'd gotten to test if it and if it worked, if he'd mark it as accepted. – Ishaan Javali Jan 12 '19 at 02:09
  • @IshaanJavali: And as * said, there is not only a waiting period required (not optional) before the poster *can* accept, and it's rather rude to try to force them to do so in any case. The user is not required to vote or accept any answer. If they choose to do so, it's their own choice and they should not be urged to make that choice unti they want to do so. – Ken White Jan 12 '19 at 02:11
  • I understand Ken. I'll make sure to give them time to test it out. I am aware that it is their choice and wholly agree with you. – Ishaan Javali Jan 12 '19 at 02:12

1 Answers1

3

The reason that you are getting this error is because finalTemp is of the type int and that can only hold integers. If temp1 is either a double or int, then you can do:

double finalTemp = (temp1 - 32) * 5 / 9 + 273.15;

This code will get the temperature as a double, (which can hold decimals).

However, if finalTemp is an int, as you initially had, it will not be possible to get it to

recognize 273.15 as a decimal.

Ishaan Javali
  • 1,711
  • 3
  • 13
  • 23
  • "If `temp1` is either a `double` or `int`, then ..." - that has absolutely nothing to do with result type of "(int or double) + double" expression - it will be double all the time... Obviously when `temp1` is `int` whole expression is pointless but that is not covered here either... - check [duplicates](https://stackoverflow.com/questions/10851273/why-does-integer-division-in-c-sharp-return-an-integer-and-not-a-float) for explanation. – Alexei Levenkov Jan 12 '19 at 02:57