How to round a number like 1678.5445 to 1678.6?.

5
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by Derek Elensar

You need to specify the problem more precisely, because 1678.6 is probably not exactly representable as a double. So the question arises as to how close a representation you are willing to accept.

The obvious solution would be to multiply the number by 10, round to the nearest integer, and then divide by 10. Is that good enough?

Or

``````int inumber;
double dnumber;
inumber = dnumber;
double difference = dnumber - inumber;
if(difference > 0.5){
inumber++;
.
.
.``````

You get the idea. But it rounds correctly only to the nearest decimal points

How to round a number like 1678.5445 to 1678.6?.

Do you really need to round it like that? What benefit would that give you? Its questionable if it should even be rounded that way.

I would try looking into the `<iomanip>` library using the `fixed` function.

See Here. Or for a more extensive search of the entire library, try looking here. ;)

This topic 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.