c++variablescout

Variable for 1865345.934 in c++?


How can I cout 1865345.934?

what variable should I use?

#include<iostream>
using namespace std;
int main(){
    price=1865345.934;
    cout << price;
    return 0;
}

If i use int the decimal does not appear on the output while if i use double it shows 1.86535e+006.


Solution

  • You can use std::setprecision() and specify how many decimal places it should print.

    Example:

    #include <iomanip>
    #include <iostream>
    
    int main() {
        double price = 1865345.934;
        std::cout << std::fixed << std::setprecision(3) << price << '\n';
    }