Set Precision for double - c++

I am trying to set the two decimal numbers for double type data entered by the user, and I have the proper header file , but the result on the display is only integer, no decimal ?
I do really appreciate any help.

You would want to use the following format.
cout << setprecision(# of places past decimal) << fixed << varName << endl;
The fixed Input output manipulator is what tells it that you are setting the precision for the number of places after the decimal point.

Related

c++ long double printing all digits with precision

Regarding my question I have seen a post on here but did not understand since i am new to C++. I wrote a small script which gets a number from user and script prints out the factorial of entered number.
Once I entered bigger numbers like 30, script does not print out all the digits.Output is like 2.652528598 E+32 however What I want is exact number 265252859812191058636308480000000. Could someone explain how to get all digits in long double.Thanks in advance
You can set the precision of the output stream to whatever you want in order to get your desired results.
http://www.cplusplus.com/reference/ios/ios_base/precision/
Here is an extract from the page, along with a code example.
Get/Set floating-point decimal precision
The floating-point precision determines the maximum number of digits to be written on insertion operations to express floating-point values. How this is interpreted depends on whether the floatfield format flag is set to a specific notation (either fixed or scientific) or it is unset (using the default notation, which is not necessarily equivalent to either fixed nor scientific).
Using the default floating-point notation, the precision field specifies the maximum number of meaningful digits to display in total counting both those before and those after the decimal point. Notice that it is not a minimum, and therefore it does not pad the displayed number with trailing zeros if the number can be displayed with less digits than the precision.
In both the fixed and scientific notations, the precision field specifies exactly how many digits to display after the decimal point, even if this includes trailing decimal zeros. The digits before the decimal point are not relevant for the precision in this case.
This decimal precision can also be modified using the parameterized manipulator setprecision.
// modify precision
#include <iostream> // std::cout, std::ios
int main () {
double f = 3.14159;
std::cout.unsetf ( std::ios::floatfield ); // floatfield not set
std::cout.precision(5);
std::cout << f << '\n';
std::cout.precision(10);
std::cout << f << '\n';
std::cout.setf( std::ios::fixed, std:: ios::floatfield ); // floatfield set to fixed
std::cout << f << '\n';
return 0;
}
Possible output:
3.1416
3.14159
3.1415900000
Notice how the first number written is just 5 digits long, while the second is 6, but not more, even though the stream's precision is now 10. That is because precision with the default floatfield only specifies the maximum number of digits to be displayed, but not the minimum.
The third number printed displays 10 digits after the decimal point because the floatfield format flag is in this case set to fixed.

Reading floating point values from a file drops all or part of the decimal part

I need to read floating-point values from a file.
Basic sample code of how I do this:
int main()
{
float number;
ifstream inputFile;
inputFile.open("testfile.dat");
inputFile >> number;
cout << number << endl;
return 0;
}
The first line in the file is: 13212.13131. But when I cout 'number' the displayed number is: 13212.1
The problem is part of the decimal gets dropped and in other cases all of it gets dropped. Why does this happen, and how can I solve this problem?
The point of reading the number from the file is to do mathematical calculations with it.
First, floating-point precision on output (for both std::cout and printf) is 6 decimal digits by default. You need std::setprecision() to get it print more digits. But you'll then get to the limit of float type.
On most systems float is IEEE-754 single precision, therefore it can only store about 7 digits of significant. The nearest to 13212.13131 is 1.3212130859375E4. If you need more precision, you must use double, which has about 15-16 digits of precision on most systems.
Read more: Is floating point math broken?
Try using std::setprecision():
cout << setprecision(14) << number << endl;
You will need to
#include <iomanip>
If that doesn't solve it you should try debugging it and see what the number actually is (13212.13131 or 13212.1).

In c++ does it start at the decimal or the whole # setprecision

In the below example the output is 3.1 so it starts at the first value.
double y = 3.14784;
cout << setprecision(2) << y;
in the following example the output precision starts at the decimal value
int x = 2;
double y = 3.0;
cout << setprecision(2) << x/y;
and yet in the following line of code - same x and y as declared above we get the precision starting not at all shown. (the only way for the below to print 6.00 is if we use fixed).
cout << setprecision(2) << x * y; // shows 6.
if we aren't using fixed - just a setprecision(n) where does that n start? because it states that its a set precision is used for decimal precision. and yet in the first example it looks at the whole double value and not just the decimal.
please advise.
thanks.
From http://www.cplusplus.com/reference/ios/ios_base/precision/
For the default locale:
Using the default floating-point notation, the precision field specifies the maximum number of meaningful digits to display in total counting both those before and those after the decimal point. Notice that it is not a minimum, and therefore it does not pad the displayed number with trailing zeros if the number can be displayed with less digits than the precision.
In both the fixed and scientific notations, the precision field specifies exactly how many digits to display after the decimal point, even if this includes trailing decimal zeros. The digits before the decimal point are not relevant for the precision in this case.
n starts from the first meaningful digits(non-zero)

use fixed precision without trailing zeros

I'm using setPrecision and fixed so I can set the precision AFTER the decimal point,
but I don't want to get something like this: 5.00000.
how can I take off the trailing zeros in an elegent way?
any specific way or should I perform string manipulations?
os << setprecision(5) << fixed << value;
Short Answer: Don't use fixed and instead set precision to whatever number you want + number of digits before decimal place.

c++ std::stream double values no scientific and no fixed count of decimals

The following code will print value of a and b:
double a = 3.0, b=1231231231233.0123456;
cout.setf(std::ios::fixed);
cout.unsetf(std::ios::scientific);
cout << a << endl << b << endl
The output is:
3.000000
1231231231233.012451
You can see that a is outputed with fixed 6 count of decimals.
But I want the output like this:
3
1231231231233.012451
How can i set flags only once, and output the above result.
The stream inserts 0s following the double because the stream's default precision for the output of floating-point values is 6. Unfortunately there is no straightforward way of checking if the double represents a whole number (so you could then only print the integral part). What you could do however is cast the value to an integer.
std::cout << static_cast<int>(a);
The default formatting for floating point numbers won't support the formats as requested. There are basically three settings you could use:
std::fixed which will use precision() digits after the decimal point.
std::scientific which will use scientific notation with precision() digits.
std::defaultfloat which will choose the shorter of the two forms.
(there is also std::hexfloat but that just formats the number in an form which is conveniently machine readable).
What you could do is to create you own std::num_put<char> facet which formats the value into a local buffer using std::fixed formatting an strips off trailing zero digits before sending the values one.