Search code examples
c++cout

How to cout int like a double


I'd like to display some integer with currency index (I mean dot and double zero) For example like here:

#include <iostream>

int main() {
    int w1=700,c1=99,c2=98;
    double noh2o=w1*(100.0-c1)/100.0;
    double w2=noh2o+noh2o/(100.0-c2)*c2;
    std::cout<<w2<<std::endl;
}

If somebody can help me I will be thankful.


Solution

  • Use std::fixed and std::setprecision.

    Try it online!

    #include <iostream>
    #include <iomanip>
    
    int main() {
        int w1=700,c1=99,c2=98;
        double noh2o=w1*(100.0-c1)/100.0;
        double w2=noh2o+noh2o/(100.0-c2)*c2;
        std::cout << std::fixed << std::setprecision(2) << w2 << std::endl;
    }
    

    Output:

    350.00