How to Cout a Float Number with N Decimal Places

Printing the correct number of decimal points with cout

With <iomanip>, you can use std::fixed and std::setprecision

Here is an example

#include <iostream>
#include <iomanip>

int main()
{
double d = 122.345;

std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout << d;
}

And you will get output

122.34

std::cout with floating number

The rounding off happens because of the C++ standard which can be seen by writing
std::cout<<std::cout.precision();

The output screen will show 6 which tells that the default number of significant digits which will be printed by the std::cout statement is 6. That is why it automatically rounds off the floating number to 6 digits.

How to print a float with 2 decimal places in Java?

You can use the printf method, like so:

System.out.printf("%.2f", val);

In short, the %.2f syntax tells Java to return your variable (val) with 2 decimal places (.2) in decimal representation of a floating-point number (f) from the start of the format specifier (%).

There are other conversion characters you can use besides f:

  • d: decimal integer
  • o: octal integer
  • e: floating-point in scientific notation

How to 'cout' the correct number of decimal places of a double value?

Due to the fact the float and double are internally stored in binary, the literal 7.40200133400 actually stands for the number 7.40200133400000037653398976544849574565887451171875

...so how much precision do you really want? :-)

#include <iomanip>    
int main()
{
double x = 7.40200133400;
std::cout << std::setprecision(51) << x << "\n";
}

And yes, this program really prints 7.40200133400000037653398976544849574565887451171875!

std::cout print all digits of float value

Assuming that the implementation uses radix 2 for the floating point,

if (std::numeric_limits<T>::radix == 2)
then writing ALL the decimal digits for ALL possible values would require:

std::cout << std::setprecision(std::numeric_limits<T>::digits - std::numeric_limits<T>::min_exponent);

I suspect that the formulation would be the same for radix == 10, but I cannot check this (no implementation at hand).

I wonder why you want to print all the decimals. If it's just to reconstruct the value unchanged, this is not necessary. Using a mixture of scientific notation with a precision std::numeric_limits<T>::max_digits10 should do the job. Otherwise, there are well known algorithm to print just enough decimal digits. They are used in the main REPL languages (see python repr, or how java, javascript, some Smalltalk, etc... print the floating point values), unfortunately, they are not part of a standard C++ library AFAIK.

How to print float to n decimal places including trailing 0s?

For Python versions in 2.6+ and 3.x

You can use the str.format method. Examples:

>>> print('{0:.16f}'.format(1.6))
1.6000000000000001

>>> print('{0:.15f}'.format(1.6))
1.600000000000000

Note the 1 at the end of the first example is rounding error; it happens because exact representation of the decimal number 1.6 requires an infinite number binary digits. Since floating-point numbers have a finite number of bits, the number is rounded to a nearby, but not equal, value.

For Python versions prior to 2.6 (at least back to 2.0)

You can use the "modulo-formatting" syntax (this works for Python 2.6 and 2.7 too):

>>> print '%.16f' % 1.6
1.6000000000000001

>>> print '%.15f' % 1.6
1.600000000000000

how to write float values with decimal points in c++

[EDIT]

In your code you print j the first time before using cout.precision(6);, also it's useless set the precision every time, try this:

cout.precision(6);
cout.setf(ios::fixed);
for(float j=0;j<10;j++)
{
cout<<j;
for(float i=0;i<0.8;i=i+0.1)
{
x=i+j;
y=exp(-x);
cout<<" "<<y;
}
cout<<"\n";
}

[OLD]

You can convert your variable to float in the cout:

void main()
{
int a = 1;
std::cout.precision(2);
std::cout << std::fixed << static_cast<float>(a) << std::endl;
}

The result will be: 1.00

How do I print a double value with full precision using cout?

You can set the precision directly on std::cout and use the std::fixed format specifier.

double d = 3.14159265358979;
cout.precision(17);
cout << "Pi: " << fixed << d << endl;

You can #include <limits> to get the maximum precision of a float or double.

#include <limits>

typedef std::numeric_limits< double > dbl;

double d = 3.14159265358979;
cout.precision(dbl::max_digits10);
cout << "Pi: " << d << endl;


Related Topics



Leave a reply



Submit