I have the following problem:
double a = 6.005; double b = 5.995;
I want to set precision of doubles 2 digits after point, for example
double c = a+b;// I would like to get 11.99 not 12.00
.
How can I do this?
I have the following problem:
double a = 6.005; double b = 5.995;
I want to set precision of doubles 2 digits after point, for example
double c = a+b;// I would like to get 11.99 not 12.00
.
How can I do this?
Just multiply by 100 and use integers then.
No, you either need to adjust all values one by one (mul by 100, take int part, div by 100), or you need to write your own MySpecialDouble class (that does the same just behind the scene).
You should probably use fixed point numbers:
Precision is one thing; rounded for display is quite another.
I think this is wrong headed. You should want all the precision you can get and worry about rounding for display when the results are complete.
UPDATE:
You should not be representing currency using doubles. Last time I looked, C++ was an object-oriented language. You should create an abstraction for Money that does the right thing and abstracts these details away from clients of the class.
You can create a Money class that manages a private representation of the currency as cents if you're working in dollars. Do all your calculations using integers and render at the end.