TheIndependentAquarius TheIndependentAquarius - 4 months ago 11
C++ Question

How to keep the precision of double variable in C++?

It is not about printing. It is about storing the value.

int x = 1234;


double y = 0.3456;


double z = x + y;


Currently
z
contains
1234.35
. I want
z
to contain
1234.3456


What can be done to achieve this?

Answer

It does contain 1234.3456. Check for yourself:

#include <cstdio>

int main()
{
    int x = 1234;
    double y = 0.3456;
    double z = x + y;

    printf("%.8f\n", z);
}

Output:

1234.34560000