Beginner's Question on Type Casting

162 Views Asked by At

I was going to use math.h on my numbers in my random number generator. It seems that I can only use the math.h functions on doubles. So:

I am trying to give "value" the value of "currentValue", or at least part of the number's value to be transferred over. I am working with a random number generator, so I do not care if the entire number is transferred. I only need part of it to work.

I have at some point:

int currentValue;
double value;

** Through code not shown their values are set. **

Later, I need part of value to go to currentValue.

currentValue = value 

I tried:

currentValue = static_cast<int>value; 

Any help would be appreciated.

Thanks for the help. The problem is solved.

2

There are 2 best solutions below

0
On

you forgot the parens:

currentValue = static_cast<int>(value); 
0
On

Don't use a cast when you don't need one. The code works perfectly fine with no cast at all.