C
C#β€’13mo ago
XLnTz

Converting int to double to output with decimals

Hello again! i got a little problem with converting int to double, i dont get it to output the decimals and i dont know where i do wrong ^^ Any tips?
public static void Main (string[] args)
{
double celsius = 0;
int fahr;

do
{
/*
+---------------------------+
| Here i input the integer! |
+---------------------------+
*/
Console.WriteLine("Write temp in fahrenheit: ");
fahr = int.Parse(Console.ReadLine());

celsius = FahrToCels(fahr);

Console.WriteLine("Your value in Celsius: " + celsius);
if (celsius < 82)
{
Console.WriteLine("Too Cold!");
}
else if (celsius > 87)
{
Console.WriteLine("Too Hot!");
}
} while (celsius < 82 || celsius > 87);
Console.WriteLine("Temp is ok! Enjoy!");

/*
+-------------------------------------+
| Here i want to output with decimals |
+-------------------------------------+
*/
Console.WriteLine("Fahrenheit: " + fahr);
Console.WriteLine("Celsius: " + celsius + "\n");
}

/*
+--------------------------------------------------------------------------------+
| Here i want to convert integer to double to output with decimals in code above |
+--------------------------------------------------------------------------------+
*/
public static double FahrToCels(int fahr)
{
double cel = (fahr - 32) * 5 / 9;
return cel;
}
public static void Main (string[] args)
{
double celsius = 0;
int fahr;

do
{
/*
+---------------------------+
| Here i input the integer! |
+---------------------------+
*/
Console.WriteLine("Write temp in fahrenheit: ");
fahr = int.Parse(Console.ReadLine());

celsius = FahrToCels(fahr);

Console.WriteLine("Your value in Celsius: " + celsius);
if (celsius < 82)
{
Console.WriteLine("Too Cold!");
}
else if (celsius > 87)
{
Console.WriteLine("Too Hot!");
}
} while (celsius < 82 || celsius > 87);
Console.WriteLine("Temp is ok! Enjoy!");

/*
+-------------------------------------+
| Here i want to output with decimals |
+-------------------------------------+
*/
Console.WriteLine("Fahrenheit: " + fahr);
Console.WriteLine("Celsius: " + celsius + "\n");
}

/*
+--------------------------------------------------------------------------------+
| Here i want to convert integer to double to output with decimals in code above |
+--------------------------------------------------------------------------------+
*/
public static double FahrToCels(int fahr)
{
double cel = (fahr - 32) * 5 / 9;
return cel;
}
75 Replies
cap5lut
cap5lutβ€’13mo ago
what is ur expection and what is ur result? nvm, i spotted the problem double cel = (fahr - 32) * 5 / 9; there it is (fahr - 32) * 5 / 9 <-- all parts of this are integers, thus u get integer divison
MODiX
MODiXβ€’13mo ago
cap5lut
REPL Result: Success
1/2
1/2
Result: int
0
0
Compile: 167.008ms | Execution: 20.851ms | React with ❌ to remove this embed.
MODiX
MODiXβ€’13mo ago
cap5lut
REPL Result: Success
1.0/2
1.0/2
Result: double
0.5
0.5
Compile: 336.144ms | Execution: 21.696ms | React with ❌ to remove this embed.
MODiX
MODiXβ€’13mo ago
cap5lut
REPL Result: Success
1/2.0
1/2.0
Result: double
0.5
0.5
Compile: 244.214ms | Execution: 22.982ms | React with ❌ to remove this embed.
cap5lut
cap5lutβ€’13mo ago
integer division truncates the stuff after the . to turn it into floating point division (either float or double) one of the values has to be a floating point number type u can either use double notation (eg 1.5), or float notation (1.5f), or cast to double or float (double)fahr or (float)fahr) each of these will force the floating point division with doubles ((double)fahr - 32) * 5 / 9 (fahr - 32.0) * 5 / 9 (fahr - 32) * 5.0 / 9 (fahr - 32) * 5 / 9.0
XLnTz
XLnTzOPβ€’13mo ago
Ohh, so i just need to put .0 after one of them and its fixed?
cap5lut
cap5lutβ€’13mo ago
yep because that will change the type of the value
XLnTz
XLnTzOPβ€’13mo ago
Ahh, stupid me -.- didnt even think of that.... 🀣 yea, now i get it.. haha, i've been staring me blind on the int argument and double ^^ thank you soooo much! πŸ™
cap5lut
cap5lutβ€’13mo ago
but be aware that its more than just adding a .0 anywhere 2 / 5 * 2.0 will result in 0 2.0 / 5 * 2 will result in 0.8
XLnTz
XLnTzOPβ€’13mo ago
so, if i add it to the first one it will result in 0 and the last one in 0.8? or it depens on what numbers i have?
cap5lut
cap5lutβ€’13mo ago
no it depends on where the division is 2 / 5 * 2.0 this is basically (2 / 5) (which results in 0) mulitplied by 2.0 because of int division
XLnTz
XLnTzOPβ€’13mo ago
so in my code, this is right
double cel = (fahr - 32) * 5 / 9.0;
double cel = (fahr - 32) * 5 / 9.0;
And it looks like this:
double cel = (fahr - 32) / 5 * 9.0;
double cel = (fahr - 32) / 5 * 9.0;
it would be messed up? Or its just me that dont understand? ^^ im not really good at math tho..
cap5lut
cap5lutβ€’13mo ago
naaah, thats messed up the point is, for the division part of ur formula there it depends on the data types (int or double) let me explain on this one: 2 / 5 * 2.0 u know that in math its "point calculation before line calculation" right? as in multiplication and division have priority over addition and substraction
XLnTz
XLnTzOPβ€’13mo ago
like, the first thing u calculate is inside ( ) then, i think * then / or the other way around..
cap5lut
cap5lutβ€’13mo ago
as in 2 + 3 * 4 = 14
XLnTz
XLnTzOPβ€’13mo ago
yea, there u start with 3 * 4 then + 2
cap5lut
cap5lutβ€’13mo ago
yep the ( ) have higher priority, so (2 + 3) * 4 = 20
XLnTz
XLnTzOPβ€’13mo ago
exactly so to get the double decimals right i always put the .0 somewhere after the division?
cap5lut
cap5lutβ€’13mo ago
and now 1 * 2 / 3, all have the same priority for the calculation so it goes from left to right
XLnTz
XLnTzOPβ€’13mo ago
yea,
cap5lut
cap5lutβ€’13mo ago
the computer will first compute 1 * 2 and then will calculate 2 / 3 so 2 / 5 * 2.0 is the same as (2 / 5) * 2.0 at 2 / 5 both values are integers, thus it uses integer division which results in 0, then it calculates 0 * 2.0, which is still 0 so to get rid of the integer division u have to change the type of either 2 or 5 in this example to get the expected 0.4 as result
XLnTz
XLnTzOPβ€’13mo ago
oh, so if i understad this right, i have to put the .0 in eighter the first number of the division or the last? as long as it is a number that is dividing?
cap5lut
cap5lutβ€’13mo ago
basically, ignore the numbers and just think about the types int / int * double -> (int / int) * double int / int (aka integer division) always results in another int to get a floating point result u have to have (at least) one of these as floating point type (i gonna smoke quick, brb, ~4-5min)
XLnTz
XLnTzOPβ€’13mo ago
yea, no problem, just trying to figure out this πŸ˜… i feel so stupid because i dont understand πŸ˜΅β€πŸ’« is it the last thing thats get calculated that's supposed to have the decimal?
cap5lut
cap5lutβ€’13mo ago
back
XLnTz
XLnTzOPβ€’13mo ago
welcome back πŸ™‚
cap5lut
cap5lutβ€’13mo ago
well, its really about the datatypes not the actual values as soon as u have int / int it will result in an int without the decimals taking (fahr - 32) * 5 / 9.0 as example thats ((fahr - 32) * 5) / 9.0 resolving that purely on data type: ((int - int) * int) / dobule (int * int) / double int / double because not both are ints at the division point, u get a double result at the end try to replace it like i did with these two 2 / 5 * 2.0 2.0 / 5 * 2 (do the first completely before starting the second)
XLnTz
XLnTzOPβ€’13mo ago
oh, so u calculate the integers first and lastly add the decimal to get decimal value?
cap5lut
cap5lutβ€’13mo ago
in ur mind basically do it in 3 steps: 1) set parenthesis to visualize which will be computed first 2) replace all values with their types 3) resolve the resulting data type if u have an integer type and a floating point type, no matter which mathematical operation u do, it will always end up with the floating point type for 2 / 5 * 2.0 yes because its going from left to right do that step by step for 2 / 5 * 2.0 if u dont know, just guess. if there is a mistake ill explain u what was wrong on that step
XLnTz
XLnTzOPβ€’13mo ago
2.0 / 5 * 2 = 0.8 and 2 / 5 * 2.0 = 0
cap5lut
cap5lutβ€’13mo ago
yep (not sure if u understood it, or if u scrolled up xD)
XLnTz
XLnTzOPβ€’13mo ago
no actually i entered both in cisual studio and tried by them self ^^
cap5lut
cap5lutβ€’13mo ago
so, did u understand it or not?
XLnTz
XLnTzOPβ€’13mo ago
2 / 5 does result in a decimal value but the console is saying 0, and i guess thats becaus i dont have a decimal on either of them, so i get that decimal value from the 3rd? or im totaly out of grid.. feels like im talking values again -.- xD damn, i feel so stupid xD
cap5lut
cap5lutβ€’13mo ago
well, in normal real life what is 2 / 5?
XLnTz
XLnTzOPβ€’13mo ago
0.4 less than 0.5 so it gets 0
cap5lut
cap5lutβ€’13mo ago
and because its integer division u have to truncate the decimals, so 0.4 becomes what? NO, not rounding, truncating! it will be just removed
XLnTz
XLnTzOPβ€’13mo ago
ohπŸ™„
cap5lut
cap5lutβ€’13mo ago
do u remember elementary school division? "the result is <blah> with a reminder of <blah2>" integer division is just that, it calculates <blah>
XLnTz
XLnTzOPβ€’13mo ago
hm, i think i need to read about math a little more... actually i dont remember.. so im gonna look it up and read.. damn, i feel so stupid... my memory sucks atm.. sorry for wasting your time by not fully understanding.. ^^
cap5lut
cap5lutβ€’13mo ago
you have 5 apples and u want to give 2 people equally apples, how many apples does each one have?
XLnTz
XLnTzOPβ€’13mo ago
i have to split them oh 2 each
cap5lut
cap5lutβ€’13mo ago
u dont have a knife, u cant split them yep thats integer division
XLnTz
XLnTzOPβ€’13mo ago
Ahh okay πŸ˜… that one i understood xD i bet ur laughing you ass off right now 🀣
cap5lut
cap5lutβ€’13mo ago
i dont laugh at you! its okay to not know/understand something. u try to change that, so u have my respect πŸ˜‰
XLnTz
XLnTzOPβ€’13mo ago
its okay if u do, because i did xD ThanksπŸ™ i really appriciate that, and your help! πŸ™
cap5lut
cap5lutβ€’13mo ago
so, did it help or do u still have questions (btw trust me, the more u learn, the more u learn that u dont understand sh_t :'D)
XLnTz
XLnTzOPβ€’13mo ago
it helped with the integer division, but the double thing, if i only have integers and division is involved i add .0 in the division part? haha thats true xD and the more i learn, the more i learn i dont understand and learn so i understand more xD
cap5lut
cap5lutβ€’13mo ago
u have to make one of the division values a float/double, as soon as that happens u got ur knife and can slice through them all well, having a knife is basically the wrong metaphor. imagine u dont have "full" apples, but incredibly thin sliced apples
XLnTz
XLnTzOPβ€’13mo ago
ahh so, if i got i right, whatever integers i have that gets divided, i add .0 to eighter one of the division values to get the float/double value? thats how i understood it ^^
cap5lut
cap5lutβ€’13mo ago
yep thats also not just a division thing 1 + 2.0 makes it a double as well basically no matter if addition, substraction, multiplication or division, as soon as a floating point value is involved, the result will be of that type thats quite easy to explain looking at byte and int
XLnTz
XLnTzOPβ€’13mo ago
Okay then i understand the integer division part πŸ™ and 1 + 2.0 is the same as 1.0 + 2? or its the highest value that gets the decimal?
cap5lut
cap5lutβ€’13mo ago
it doesnt matter which of the both it is, its a matter of if any of it is that part is easier explainable with int and byte
byte a = 1;
int b = 255;
var c = a + b;
byte a = 1;
int b = 255;
var c = a + b;
XLnTz
XLnTzOPβ€’13mo ago
so its basically division thats the part i have to look out for?
cap5lut
cap5lutβ€’13mo ago
yeah integer division can f_ck up ur math if u have floats/doubles involved its a precision thingy, if 2 different number types are involved, the higher precision takes place
XLnTz
XLnTzOPβ€’13mo ago
ohh, okay, i think i got it now then πŸ˜ƒ Damn, need same explenation as a 6 year old xD yea, with the understanding i have now i get that the division is the big bully in math ^^
cap5lut
cap5lutβ€’13mo ago
c is here an int, because its an byte (0-255) and an int (-2b to 2b roughly) its not directly math, its about the different types an integer simply cant represent 1/2 so u need to somehow "upgrade" the data type, so its representable. and that u do by making one of the operands that upgraded data type ciggy time ;p
XLnTz
XLnTzOPβ€’13mo ago
integers are integers, they dont have anything to do with the decimals until the division gets there, so its possible to add the decimal to whatever integer i want as long as division's not there? ^^
cap5lut
cap5lutβ€’13mo ago
it depends on the result u want
XLnTz
XLnTzOPβ€’13mo ago
if i want decimal result i add .0 and if i dont i dont add it? ^^
cap5lut
cap5lutβ€’13mo ago
if u want sliced apples, slice one apple before dividing them if u only want "complete" apples, dont slice them slicing = converting to float/double okay, time for an excercise! 3 / 2 * 2.0, whats the result?
XLnTz
XLnTzOPβ€’13mo ago
and thats where .0 comes in to play, i add it where ever the result of eighter calculation gets decimal outcome? 2 because the .0 is not on the first 3 or 2 ?
cap5lut
cap5lutβ€’13mo ago
to be correct, the result would be actually 2.0 in a sense, because its not an integer anymore, right? because both 3 and 2 are integers, yes there are 3 variants to make 3 / 2 * 2.0 result in 3.0 which are they? (write the whole formula)
XLnTz
XLnTzOPβ€’13mo ago
oh, my adhd-brain is spinning too much right now xD
cap5lut
cap5lutβ€’13mo ago
dont worry im stubborn ;p just in words, what would u have to change to make it result in 3.0? (u can write the formulas instead ofc)
XLnTz
XLnTzOPβ€’13mo ago
haha i actually dont know xD one is over after dividing 3 by to, then 1 * 3 is 3 3 by 2 haha i need to take a little break atm, wifey is kind of hangry too so i have to get her to get food xD but i'll let u know when im back and we can keep going if u want ^^
cap5lut
cap5lutβ€’13mo ago
uhmmm , its quite late for me can we continue this in 6h or later? i sorta want to sleep xD
XLnTz
XLnTzOPβ€’13mo ago
absolutely xD we can do a little tomorrow if u want to ^^ thanks for all the help! i really appriciate it! πŸ™
cap5lut
cap5lutβ€’13mo ago
well, u can continue without me, maybe someone else will step in worst case u write some stuff and i will react to it after i woke up πŸ˜‰
XLnTz
XLnTzOPβ€’13mo ago
hahaha yea xD
cap5lut
cap5lutβ€’13mo ago
and greet ur wife from that drunkard stranger on discord πŸ˜‚
XLnTz
XLnTzOPβ€’13mo ago
Hahaha, yea, i will xD well, gotta go, thanks again for all the help! πŸ™
JP
JPβ€’13mo ago
I think ya'll landed in a good spot! I tried my hand at writing up the rules here https://gist.github.com/John-Paul-R/40ef1b86a376f2838a3fa6fdab9c7cc7 Might be helpful, might not. Use it if its useful!
cap5lut
cap5lutβ€’13mo ago
didnt even know about the d prefix for doubles πŸ˜‚ but i think for a generic explanation its too much about actual values. as intro to describe the problem, yes but its for example missing promotions like byte to int or float to double (basically in the same numberic system, just different precision) (the whole thing sorta sounds like its just about integers vs floating points) but generally speaking its a good start and now i really hit the sack >.<
JP
JPβ€’13mo ago
yeah for the list of promotions, I tossed in an article describes all of them, figured I'd spare rewriting the thing, got long enough already, yknow?
C# numeric promotions
Color Of Code - website about software and programming
Want results from more Discord servers?
Add your server