Jump to content

  • Log In with Google      Sign In   
  • Create Account


Converting Variables


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 acw83   Members   -  Reputation: 174

Like
Likes
Like

Posted 15 December 1999 - 04:20 AM

What will result from this:

int x;
float y;

y = 45.54525252552
x = y;

Will x be rounded to 46, or will the fraction just get chopped? And if it is chopped, how can I force it to round up if the fraction is greater than or equal to 1/2?


Sponsor:

#2 Melo   Members   -  Reputation: 122

Like
Likes
Like

Posted 12 December 1999 - 09:07 AM

I've learned that the reslt will be 45, but I've never tested it.
If you want to round it, you have to write the following:
code:

float a = 45.426;
int b;

b = a + 0.5;


This should work... BYE


#3 Electron   Members   -  Reputation: 122

Like
Likes
Like

Posted 13 December 1999 - 12:55 AM

As a complement to the post above, i can confirm that the value will be truncated, not rounded.

#4 CJ   Members   -  Reputation: 122

Like
Likes
Like

Posted 13 December 1999 - 10:10 AM

Indeed it will be set to 45. Because it just chops the fraction off of it.......

------------------
Dance with me......


#5 acw83   Members   -  Reputation: 174

Like
Likes
Like

Posted 13 December 1999 - 10:14 AM

Thanks for the help.

#6 MikeD   Members   -  Reputation: 158

Like
Likes
Like

Posted 15 December 1999 - 04:20 AM

Also, if you want to make sure of a value being set to one side or other of an integer you can use
double floor(double)...rounds down
and
double ceil(double)...rounds up
from Math.h




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS