Hi, I have read about using sprintf and tried it but can't make sense of how it works.

I have a double with the value 16.525 .
I want to cout only 16.53

I also tried setprecision() and it just cuts off the 5 on the end which won't display larger numbers right. I mean I want two decimal places to show because this is a dollar amount.

can someone show me how to do this?

Thanks

## All 7 Replies

One thing you can do is add .005 to the double before hand. If the 3rd decimal is 5 or more, it will round it up, if not, it will keep it at the number it's at. That's one quick and dirty way to handle it. Then just use setprecision() to cut off the excess numbers and you should be fine.

Hey,

I was trying to write something to help you and got confused myself by this output. May be someone can take a look at this too. Its all related so I'm not starting a new thread

``````#include <iostream>
#include <iomanip>

int main()
{
std::cout << std::setprecision(2) << std::fixed << 12.525 << std::endl;
std::cout << std::setprecision(2) << std::fixed << 16.525 << std::endl;
}``````

output:
12.53
16.52

How is it possible that setprecision is rouding 12.525 to 12.53 but 16.525 to 16.52 :-O !!!

Hey,

I was trying to write something to help you and got confused myself by this output. May be someone can take a look at this too. Its all related so I'm not starting a new thread

``````#include <iostream>
#include <iomanip>

int main()
{
std::cout << std::setprecision(2) << std::fixed << 12.525 << std::endl;
std::cout << std::setprecision(2) << std::fixed << 16.525 << std::endl;
}``````

output:
12.53
16.52

How is it possible that setprecision is rouding 12.525 to 12.53 but 16.525 to 16.52 :-O !!!

The point is that floating point data are NOT fixed point data. For example, the double type constant 15.525 is NOT equal to 15525/1000 (in math sense), and 16.525 constant is NOT equal to 16525/1000.

Your example shows that actually double(16.525) < 16525/1000 (on your computer) so the best rounding toward zero is 16.52.

See DemonGal711's suggestion in the post above (and don't try to write banking software until you understand floating point data features ;))...

The problem with using setprecision() as I tried to point out in my original post is that it doesn't work if the number is different.

If I use setprecicion(4) for 16.252 it will work displaying 16.25

But the next time the program is run the variable value might be 216.252 and it will end up displaying 216.2 instead of 216.25 so set precision is just cutting off at 4 which doesn't work when the number can vary in size.

Set fixed before setprecision. The last manipulator defines the number of digits after the decimal point in fixed notation only (otherwise the total number of significant digits).

Yes thank you arkM

The examples I looked at didn't have the fixed manipulator in them. I just found this out at school talking to another student.

Thanks

Hey,
output:
12.53
16.52

How is it possible that setprecision is rouding 12.525 to 12.53 but 16.525 to 16.52 :-O !!!

If you display the values with a large precision, you'll see that what's actually stored is:
12.52500000000000000000
16.52499999999999900000

That explains your second value being rounded down.

Be a part of the DaniWeb community

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