3
Contributors
13
Replies
14
Views
5 Years
Discussion Span
Last Post by fadi_1234
0

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

0

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.

0

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

0

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

0

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.

1

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.

0

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.