decimals

• 04-15-2003
KJ_Magic
decimals
hello,

I was wondering if there is a way take variables that have a decimal and be able to set thier value to somthing else...

for example: if x=4.4 i want to change it to x=0, however if x=4 i want to keep it that way...

i would explain why i need to do this, but the explaintion would be very long...

Thanks,
KJ
• 04-15-2003
DirX
Here is one simple way that should work for asfar as i know:

Code:

``` double x = 4.4; doube y = x - (int)x; if( (y > -0.51 && y < 0.51) && y != 0.0 )     x = 0.0;```
For asfar as i was able to think it through it seemed pretty bullet-proof to me. What it does is it subtracts the rounded integer value of x from x. Considering x can be rounded up or down at most 0.5 and when the final is 0.0 the rounded value is the same as the original it should work. There is most probably an easier way to do this that i am not aware of ;)
• 04-15-2003
RoD

Code:

`cout << setiosflags(ios::fixed | ios::showpoint | ios::right) << setprecision(2);`
before you output anything to ensure the decimal shows up.
• 04-15-2003
alpha
an int cast will not round the value, it will truncate it.

so if you have x = 4.7 for example, you will subtract 4.7 - 4 . which would give you .7 . this isn't in your if statement.
• 04-15-2003
DirX
OK, my bad, it was my believe that when making an double/float an int it would roud it either up or down conidering the disance away from the closest full integer, not always down. If what you are saying is true you would simply have to change the code as folloing:

Code:

```double x = 4.4; doube y = x - (int)x; if( y > 0.0 )     x = 0.0;```
• 04-15-2003
alpha
Quote:

Originally posted by DirX
If what you are saying is true
it is. think about it this way... in integer math, since it is truncated; this would give you zero, not one, or whatever 4 / 7 is in decimal form.

Code:

```int x = 4, y = 7; double number = x / y;```
• 04-15-2003
DirX
Yep alpha you are indeed right so KJ_Magic use the code in the latest post not that in the first one, and my apoligees for any confusion i might have caused. Those floor and ceiling functions in programming class must have messed me up a bit ;)
• 04-15-2003
KJ_Magic
thanks, i was also wonding if there is a way that i could only have non decimals be outputed...

for example: x=4.5, y=5, h=3.56, g=34.3; i only want y to be outputed because it is the only non decimal

-KJ
• 04-15-2003
alpha
cout << y;

but if you are asking for input, then just use if statements.
• 04-16-2003
KJ_Magic
Alpha,

int x = 4, y = 7;
double number = x / y;

but it didnt work, it worked out to 1 every time, i tried changing it around but i still got 1, 1 didnt work becuase i need ones that equal one to be used.

-KJ
• 04-16-2003
alpha
Quote:

Originally posted by KJ_Magic
Alpha,

int x = 4, y = 7;
double number = x / y;

but it didnt work, it worked out to 1 every time, i tried changing it around but i still got 1, 1 didnt work becuase i need ones that equal one to be used.

-KJ

first of all, it should come out to zero, not one. and for your original task, use DirX's post above mine. I just used that as an example of integer math.
• 04-16-2003
Code:

```x=4.5, y=5, h=3.56, g=34.3 double array[4] = {4.5, 4, 3.56, 34.3}; double diff; int i; for(i = 0; i < 4; ++i) {   diff = i - (int)i;   if(i == 0)     cout i << endl; }```
Do you consider 4 to be the same as 4.0 or not? If not, then you will need to convert the value into a string and search the string for a decimal point/period.
• 04-16-2003
KJ_Magic
ok, i see what you mean.. now my program works

Code:

```#include<iostream.h> #include<fstream.h> int main() {         double c, h, s, d, i, r;         i=1;         r=0;         while(r<52)         {                 r=r+1;                 cout<<endl<<r<<". ";                                 i=r;                 i=i+5;                 c=i/4;                 i = c - (int)c;                         if( i > 0.0 )                                 c = 0.0;                         if(c>13)                                 c=c-13;                         if(c!=0)                                 cout<<"C: "<<c;                                 i=r;                 i=i+10;                 h=i/4;                 i = h - (int)h;                         if( i > 0.0 )                                 h = 0.0;                         if(h>13)                                 h=h-13;                         if(h!=0)                                 cout<<"H: "<<h;                         i=r;                 i=i+15;                 s=i/4;                 i = s - (int)s;                         if( i > 0.0 )                                 s = 0.0;                         if(s>13)                                 s=s-13;                         if(s!=0)                                 cout<<"S: "<<s;                 i=r;                 i=i+0;                 d=i/4;                 i = d - (int)d;                         if( i > 0.0 )                                 d = 0.0;                         if(d>13)                                 d=d-13;                         if(d!=0)                                 cout<<"D: "<<d;         }         return 0; }```
but no i have a new problem! The program is made to repeat its self 52 times so when i see the output all i can see is the last 20 or so outputs...

and incase you are wondering this program is to caculate the order of a deck of cards so: c=clubs, s=spades, h=hearts, d=dimonds. also since there are 52 cards in a deck, the caculaton needs to be run 52 times.

if you see any thing in my code that shouldnt be there, please tell me. thanks

-KJ Magic
• 04-16-2003
rather than

cout<<endl<<r<<". ";

you could try:

cout << "-" << r << "-";

Good news: you'll probably be able to see the entire output.
Bad news: it won't be so neat.

Compromise: someplace between new line for every time through loop and no new lines at all. You can experiment until you feel comfortable with it