I need to use digits after decimal point for my next calculations but I don't know how!
this is what i came up with:
(this program gets user input(distance) and divide it by 111(g1 and g2).)
1 2 3 4 5 6 7
int distance,g1;
double g2,sub;
cin>> distance;//example:200
g1=(abstand/111); //200/111=1 (int)
g2=(abstand/111);//200/111=1,80180 (double)
sub=(g2-g1); //what I need : 0,80180
but this code doesn't work! (sub=0)
can you find the problem?
or is there a better way?
( i hope it's clear what i mean)
I don't know what the type or value of abstand is. However, if it is an int, since 111 is an int then integer division will be done, giving an integer result. Storing that result afterwards in a type double doesn't change that, since the calculation has already been completed.
If either or both of the values used in the calculation are floating-point-type, then floating-point division will be done. Here for example, use 111.0 (double) instead of 111 (int).