C
C#

Converting int to double to output with decimals

# Converting int to double to output with decimals

XXLnTz11/18/2023
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;    }
Ccap5lut11/18/2023
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
MMODiX11/18/2023
Ccap5lut11/18/2023
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
XXLnTz11/18/2023
Ohh, so i just need to put .0 after one of them and its fixed?
Ccap5lut11/18/2023
yep because that will change the type of the value
XXLnTz11/18/2023
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! 🙏
Ccap5lut11/18/2023
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
XXLnTz11/18/2023
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?
Ccap5lut11/18/2023
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
XXLnTz11/18/2023
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..
Ccap5lut11/18/2023
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
XXLnTz11/18/2023
like, the first thing u calculate is inside ( ) then, i think * then / or the other way around..
Ccap5lut11/18/2023
as in 2 + 3 * 4 = 14
XXLnTz11/18/2023
Ccap5lut11/18/2023
yep the ( ) have higher priority, so (2 + 3) * 4 = 20
XXLnTz11/18/2023
exactly so to get the double decimals right i always put the .0 somewhere after the division?
Ccap5lut11/18/2023
and now 1 * 2 / 3, all have the same priority for the calculation so it goes from left to right
XXLnTz11/18/2023
yea,
Ccap5lut11/18/2023
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
XXLnTz11/18/2023
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?
Ccap5lut11/18/2023
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)
XXLnTz11/18/2023
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?
Ccap5lut11/18/2023
back
XXLnTz11/18/2023
welcome back 🙂
Ccap5lut11/18/2023
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)
XXLnTz11/18/2023
oh, so u calculate the integers first and lastly add the decimal to get decimal value?
Ccap5lut11/18/2023
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
XXLnTz11/18/2023
2.0 / 5 * 2 = 0.8 and 2 / 5 * 2.0 = 0
Ccap5lut11/18/2023
yep (not sure if u understood it, or if u scrolled up xD)
XXLnTz11/18/2023
no actually i entered both in cisual studio and tried by them self ^^
Ccap5lut11/18/2023
so, did u understand it or not?
XXLnTz11/18/2023
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
Ccap5lut11/18/2023
well, in normal real life what is 2 / 5?
XXLnTz11/18/2023
0.4 less than 0.5 so it gets 0
Ccap5lut11/18/2023
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
XXLnTz11/18/2023
oh🙄
Ccap5lut11/18/2023
do u remember elementary school division? "the result is <blah> with a reminder of <blah2>" integer division is just that, it calculates <blah>
XXLnTz11/18/2023
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.. ^^
Ccap5lut11/18/2023
you have 5 apples and u want to give 2 people equally apples, how many apples does each one have?
XXLnTz11/18/2023
i have to split them oh 2 each
Ccap5lut11/18/2023
u dont have a knife, u cant split them yep thats integer division
XXLnTz11/18/2023
Ahh okay 😅 that one i understood xD i bet ur laughing you ass off right now 🤣
Ccap5lut11/18/2023
i dont laugh at you! its okay to not know/understand something. u try to change that, so u have my respect 😉
XXLnTz11/18/2023
its okay if u do, because i did xD Thanks🙏 i really appriciate that, and your help! 🙏
Ccap5lut11/18/2023
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)
XXLnTz11/18/2023
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
Ccap5lut11/18/2023
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
XXLnTz11/18/2023
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 ^^
Ccap5lut11/18/2023
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
XXLnTz11/18/2023
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?
Ccap5lut11/18/2023
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;
XXLnTz11/18/2023
so its basically division thats the part i have to look out for?
Ccap5lut11/18/2023
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
XXLnTz11/18/2023
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 ^^
Ccap5lut11/18/2023
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
XXLnTz11/18/2023
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? ^^
Ccap5lut11/18/2023
it depends on the result u want
XXLnTz11/18/2023
if i want decimal result i add .0 and if i dont i dont add it? ^^
Ccap5lut11/18/2023
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?
XXLnTz11/18/2023
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 ?
Ccap5lut11/18/2023
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)
XXLnTz11/18/2023
oh, my adhd-brain is spinning too much right now xD
Ccap5lut11/18/2023
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)
XXLnTz11/18/2023
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 ^^
Ccap5lut11/18/2023
uhmmm , its quite late for me can we continue this in 6h or later? i sorta want to sleep xD
XXLnTz11/18/2023
absolutely xD we can do a little tomorrow if u want to ^^ thanks for all the help! i really appriciate it! 🙏
Ccap5lut11/18/2023
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 😉
XXLnTz11/18/2023
hahaha yea xD
Ccap5lut11/18/2023
and greet ur wife from that drunkard stranger on discord 😂
XXLnTz11/18/2023
Hahaha, yea, i will xD well, gotta go, thanks again for all the help! 🙏
JJP11/19/2023
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!
Ccap5lut11/19/2023
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 >.<
JJP11/19/2023
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?

## Looking for more? Join the community!

C
C#

Converting int to double to output with decimals

Join Server