I'm brand new to programming so please answer in simple terms. I'm trying to print a double. The program ask the user to input as many digits of pi as he can remember, and then it is supposed to print it back to him. But it always prints back 6 decimal places. I need it to print the amount of decimals that were originally put in. so 3.14 is to decimals, while 3.141592654 is 9. so it prints what was put in.
-
2Please post your code and example input and output. – MC93 Sep 14 '15 at 12:20
-
4Why is this tagged [tag:c]? Where is your code? – Lightness Races in Orbit Sep 14 '15 at 12:20
-
5`C` and `C++` are different languages, with different input and output considerations. Which one are you interested in? – abelenky Sep 14 '15 at 12:21
-
1use a [`std::string`](http://en.cppreference.com/w/cpp/string/basic_string) – NathanOliver Sep 14 '15 at 12:29
-
[Set the digits after decimal point](http://stackoverflow.com/q/3923202/995714) – phuclv Sep 14 '15 at 12:38
4 Answers
You need a combination of std::fixed followed by std::precision
Like this:
double f =3.14159;
std::cout << std::fixed;
std::cout << std::setprecision(5) << f << '\n'; // prints 5 decimals
std::cout << std::setprecision(9) << f << '\n'; // prints 9 decimals
Now what to pass to std::setprecision()
as an argument needs to be calculated from the input you are getting.

- 9,486
- 12
- 49
- 67
-
3I don't understand how this was upvoted four times, as it clearly does not answer the question... "I need it to print the amount of decimals that were originally put in" - Your solution clearly doesn't do that... – leemes Sep 14 '15 at 12:37
Numbers are numbers, not strings. They have actual precision governed by their type (float
, double
etc).
You cannot "remember" the logical precision originally provided, unless you take input as string, count the precision yourself, then convert to a number.
Then you use formatting options to reproduce that precision level in the output.
Alternatively, just stick with strings.

- 378,754
- 76
- 643
- 1,055
For this specific case only (comparing how precise was the entered pi), use a std::string
rather than a double. If someone who knows a couple of thousands of digits uses your program, the double-precision floating point variable will not be precise enough for a comparison. You will have to store a reference pi as a string too.
Incidentally, with this approach you will no longer have the issue of remembering how many decimal places were entered.

- 3,147
- 18
- 28
You might want to investigate the differences between decimal and binary representations of numbers.
Your user enters a decimal representation, i.e. each symbol represents a digit from 0 to 9.
On the other hand, a double is a binary representation.
Some numbers can be expressed with a decimal representation but have no equivalent in binary representation. For instance you cannot express 0.3 with a finite number of binary symbols.
That's why you should keep your input as decimal, for instance by using a string.

- 7,244
- 3
- 31
- 39