Hej

Its too simple looking but I need c++ code to round off any floating point number to the nearest 1000th. I read somewhere to do it like this:

double number = cos(theta);
double rounded_1000th = floor((x*1000.0 + 0.5)/1000.0);

But this solution fails for numbers less than zero e.g., it rounds 0.70712 to 0.0 instead of 0.707
Can anyone help?

You want to floor then /1000.0

You are dividing by 1000 then taking the floor.

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.