How to round a number like 1678.5445 to 1678.6?.

Recommended Answers

All 5 Replies

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. ;)

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.