how to transform from double to int (rounded) ?
by using basic stuff ?

Recommended Answers

All 13 Replies

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.

i cant use these method is there more simple formula ?

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'm not quite sure how to frame it in a way that it will be acceptable to your instructor.

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

is there any way by using modulus

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

As I said before:

double a = 1.3; //everything up to 1.9999999etc. will give you 1
int i = a;

can you give me example of how we use it ?

bro thanks it work :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.