John Paul Rodrigues
John Paul Rodrigues

Reputation: 51

Why is c++ rounding out float value in cout

I'm starting to learn cpp and am trying some of the cpp questions on hackerrank.com as a practice but I'm not using their compiler but I'm using visual studio 2019.

The issue is 19856.992 is printing as 19857 and -5279235.721231465 as -5.27924e+06.

Code:

#include<iostream>

int main() {
    int intVal;
    long long int LongIntVal;
    char CharVal;
    float FloatVal ;
    double DoubleVal;
    std::cout << "input: ";
    std::cin >> intVal >>LongIntVal>>CharVal>>FloatVal>>DoubleVal;
    std::cout << std::endl << intVal << std::endl << LongIntVal << std::endl<< CharVal << std::endl << FloatVal << std::endl << DoubleVal;
    return 0;
   
}

output:

input: 211916801 97592151379235457 p 19856.992 -5279235.721231465

211916801
97592151379235457
p
19857
-5.27924e+06

Upvotes: 4

Views: 2010

Answers (2)

Barrnet Chou
Barrnet Chou

Reputation: 1923

I suggest you could output the value you want by calculating the decimal places of the floating point number and combining the function setprecision.

For example:

int foo(float f)
{
    int i = 0;
    while (i++, f != (int)f) f *= 10;
    return i - 1;
}

int main()
{
    float f = 221.444356;
    int n = foo(f);
    cout << setiosflags(ios::fixed);
    std::cout << n << std::endl << std::setprecision(n) << f;
}

Ouput:

5
221.444356

cout.precision(n), setprecision(n) can control the number of floating-point numbers displayed in the output stream. The C++ default stream output value valid bit is 6.

If setprecision(n) is combined with setiosflags(ios::fixed), you could control the number of digits to the right of the decimal point. setiosflags(ios::fixed) is a fixed-point representation of real numbers.

If it is combined with setiosflags(ios::scientific), you could control the number of digits after the decimal point of the coefficient in the exponential representation. setiosflags(ios::scientific) is an exponential representation of real numbers.

If setprecision(n) is set, but setiosflags(ios::fixed) is not set, it means the total number of digits when displaying regular floating-point numbers.

If setprecision(n) is set, but setiosflags(ios::scientific) is not set, when the floating-point number is large, the system automatically displays the exponential form, which refers to the total number of digits in the coefficient

Upvotes: 1

Tharun K
Tharun K

Reputation: 1180

You can make use of std::setprecision() function under iomanip header file

I have altered your code so that you can see how to make use of it:

#include <iostream>
#include <iomanip>
int main()
{
    int intVal;
    long long int LongIntVal;
    char CharVal;
    float FloatVal;
    double DoubleVal;
    std::cout << "input: ";
    std::cin >> intVal >> LongIntVal >> CharVal >> FloatVal >> DoubleVal;
    std::cout << std::endl
              << intVal << std::endl
              << LongIntVal << std::endl
              << std::setprecision(20) << CharVal << std::endl
              << std::setprecision(20) << FloatVal << std::endl
              << std::setprecision(20) << DoubleVal;
    return 0;
}

Output:

input: 211916801 97592151379235457 p 19856.992 -5279235.721231465

211916801
97592151379235457
p
19856.9921875
-5279235.7212314652279

Upvotes: 1

Related Questions