5 Years
Discussion Span
Last Post by fadi_1234

Use a cast?
C++ style:

double a = 1.40;
int x = static_cast<int>(a);

"C style" (still valid)

double a = 1.40;
int x = (int)a;

You will lose all digits after the decimal and it doesn't round, just truncates.

Edited by jonsca: n/a


i cant use these method

I suppose you could just assign the double value to an int, but the cast makes your intentions much clearer.


i used this formulas rounded=(int)(diff+0.99); but teacher told us that we cant use it cause we didnt take it in university yet i want more simple


i want the simpleset way
we can use double int if numbers .. but in a simplest way :)cause im new on c++ i cant use advanced way


You can't use the modulus on doubles, there is a function called fmod for that, but I don't know if that's going to get you any further along. Let the thread sit for a bit and see if someone remembers something I'm missing.


Using static_cast isn't really an advanced way. I mean, if you want the simplest then following the advice to simply assign it to an integer variable should suffice.

You can always add a comment explaining what it does, just to make your intentions clear.


i want that for example : number=1.3 give me 1
i want to round the numbers
and i want the simplest way

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.