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.
Related
Is there any elegant solution using the std C++ or Boost libraries to output a double to std::cout in a way that the following conditions are met:
scientific notation is disabled
the precision for the decimal part is 6
however, trailing 0's (for the decimal part) are not printed out
For example:
double d = 200000779998;
std::cout << `[something]` << d;
should print out exactly 200000779998. [something] should possibly be a noexcept combination of some existing manipulators.
This is not a solution to the problem:
std::cout << std::setprecision(6) << std::fixed << d;
because it prints out 200000779998.000000 with trailing 0's
Instead of using the fixed manipulator, you can try to use (abuse?) defaultfloat. As far as I understand, it chooses either fixed or scientific based on the ability to put the number within the specified precision. As a result you can set the precision to the number of digits of the integral part + the requested fractional precision (6 in your case).
double d = 200000779998;
std::cout << std::setprecision(integralDigits(d) + 6) << d << std::endl;
You can try it here.
Hard to prove a negative, but I would assume no.
The requirements are inconsistent with any normal use. Space efficiency dictates a binary format. 6 digits (decimal) of precision suggests a format intended for human readers, who can't churn through lots of data. And humans have no issue dealing with a consistent 6 digit format.
So, you're basically targeting a format that has no obvious audience, and that is why I would be surprised if there is support for that.
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.
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.
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.
I'm trying to display number in standard notation
for example:
float f = 1230000.76
turns out to be,
1.23e+006
There are two things found in iomanip that must be included. First is fixed and the second is setprecision
You need to write:
std::cout << fixed;
std::cout << setprecision(2) << f;
fixed disables the scientific notation i.e. 1.23e+006 and fixed is a sticky manipulator so you need to disable it if you want to revert back to scientific notation.
Use -
cout.setf(ios::fixed, ios::floatfield);
cout.setf(ios::showpoint);
before printing out the floating point numbers.
More information can be found here.
You can also set output precision with the following statement -
cout.precision(2);
or simply with -
printf("%.2f", myfloat);