Thread: decimals

  1. #1
    KJ_Magic
    Guest

    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

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    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

  3. #3
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    remember to add

    Code:
    cout << setiosflags(ios::fixed | ios::showpoint | ios::right) << setprecision(2);
    before you output anything to ensure the decimal shows up.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    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.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    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;

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    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;

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    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

  8. #8
    KJ_Magic
    Guest
    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

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    cout << y;

    but if you are asking for input, then just use if statements.

  10. #10
    KJ_Magic
    Guest
    Alpha,

    i tried your code

    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

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by KJ_Magic
    Alpha,

    i tried your code

    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.

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.

  13. #13
    KJ_Magic
    Guest
    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

  14. #14
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help to show decimals
    By engstudent363 in forum C Programming
    Replies: 4
    Last Post: 02-19-2008, 04:13 PM
  2. allow decimals
    By dankassdann in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2006, 06:41 PM
  3. multiple decimals = problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-18-2002, 08:32 AM
  4. Decimals To Binary
    By kas2002 in forum C++ Programming
    Replies: 8
    Last Post: 06-08-2002, 11:12 AM
  5. Decimals to Fractions
    By ToasterPas in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2001, 12:58 PM