# quick help with double

This topic is 5160 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

not new but freshening up on my c++ when using a double, how do you tell the computer you only want 2 decimal places? Example "You made 143.234485 dollars yesterday" vs. "You made 143.23 dollars yesterday"

##### Share on other sites
#include <iostream>using namespace std;int main(int argc, char* argv[]) {        cout.setf(ios::fixed); // use a fixed number of decimals. 	cout.precision(2); //make it 2 decimal places	cout << 0.444444;        return 0;}

##### Share on other sites
Thanks, thats what i needed.

##### Share on other sites
or have a look at iomanipulators (see the setw example)

##### Share on other sites
You should not use double to do that kind of thing
Store the amount in cents

One could write a decimal class

template<int nbDecimals, class Int = int, int base=10>class Decimal{public: Decimal(Int i): value(i*decalage) { } Decimal(Int i, Int d) : value(i*decalage+d) { assert(d>=0 && d<decalage); } //Define all the operators (=, +, *...)private: Int value; static const Int decalage = Power<base, nbDecimals>::value;}template<int v, int n>struct Power{ static const int value = Power<v, n-1>::value * v;}template<int v>struct Power<v, 0>{ static const int value = 1;}

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 13
• 76
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632968
• Total Posts
3009585
• ### Who's Online (See full list)

There are no registered users currently online

×