real simple question - decimals
-
hey all, i have a really simple question, im amazed i cant find out how to do this. i have 2 integers, called
x
andy
. i need to showprintf("%d", (x/y));
everything i try will round it to the closest integer. i've tried usingdouble
for x,y, another variable that stores the value of x/y, but it still rounds the to the closest integer. how can i display a number not rounded to the closest integer? thanks in advance -
hey all, i have a really simple question, im amazed i cant find out how to do this. i have 2 integers, called
x
andy
. i need to showprintf("%d", (x/y));
everything i try will round it to the closest integer. i've tried usingdouble
for x,y, another variable that stores the value of x/y, but it still rounds the to the closest integer. how can i display a number not rounded to the closest integer? thanks in advanceYou also need to change
%d
to%lf
to tellprintf()
that it should output adouble
, not anint
.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
-
hey all, i have a really simple question, im amazed i cant find out how to do this. i have 2 integers, called
x
andy
. i need to showprintf("%d", (x/y));
everything i try will round it to the closest integer. i've tried usingdouble
for x,y, another variable that stores the value of x/y, but it still rounds the to the closest integer. how can i display a number not rounded to the closest integer? thanks in advance -
hey all, i have a really simple question, im amazed i cant find out how to do this. i have 2 integers, called
x
andy
. i need to showprintf("%d", (x/y));
everything i try will round it to the closest integer. i've tried usingdouble
for x,y, another variable that stores the value of x/y, but it still rounds the to the closest integer. how can i display a number not rounded to the closest integer? thanks in advanceSee
double c; double x = 123.456; double y = 2.1; c=x/y; printf( "%f\n", c);//output->58.788571
_**
**_
whitesky
-
You also need to change
%d
to%lf
to tellprintf()
that it should output adouble
, not anint
.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
If both of the numbers are of type int like they stated then you'll get (On Visual C++ 6.0 and VC++ 2003) #include "stdafx.h" int main(int argc, char* argv[]) { int x=1; int y=9; printf("%f\n",x/y); // The expression gets truncated before formatting printf("%lf\n",x/y); // The expression gets truncated before formatting printf("%f\n",(double)x/y); // The correct way when both are integers printf("%lf\n",(double)x/y); // The correct way when both are integers return 0; } *************** OUTPUT VC++ 6.0******************* -1.#QNAN0 -1.#QNAN0 0.111111 0.111111 Press any key to continue *************** OUTPUT VC++ 6.0******************* 796482944349676280000000000000000000000000000000000 000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000 000000000000000000000000000000.000000 796482944349676280000000000000000000000000000000000 000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000 000000000000000000000000000000.000000 0.111111 0.111111 Press any key to continue
-
See
double c; double x = 123.456; double y = 2.1; c=x/y; printf( "%f\n", c);//output->58.788571
_**
**_
whitesky
-
One slight correction: Use the "
%f
" format code for values of typefloat
, and "%lf
" format code for values of typedouble
.
Software Zen:
delete this;