0

I want to display 265612.2 how ever the output still 265612

I input : 4.2

#include <iostream>

using namespace std;

int main()
{
    double a;
    double b;
  
    cout<<"Enter the number of light year : ";
    cin>>a;
    b = a * 63241;
    cout<<b;

    return 0;
}
Dario Petrillo
  • 980
  • 7
  • 15
Bajas
  • 9
  • Does this answer your question? [std::cout with floating number](https://stackoverflow.com/questions/33854825/stdcout-with-floating-number) – DSC Mar 27 '21 at 09:20

1 Answers1

0

You need to "manipulate" the output stream (std::cout):

#include <iomanip>     // manipulating helpers
#include <iostream>

int main() {

    double a;
    double b;

    std::cout<<"Enter the number of light year : ";
    if(std::cin >> a) {
        std::cout << std::setprecision(1)  // one decimal
                  << std::fixed;           // fixed precision

        b = a * 63241.01;

        std::cout << b;
    }
}

Demo

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • Sorry for the wrong details. I've already edit some of my code. I want to display 265612.2 how ever the output still 265612 I input : 4.2 #include using namespace std; int main() { double a; double b; cout<<"Enter the number of light year : "; cin>>a; b = a * 63241; cout< – Bajas Mar 27 '21 at 09:22
  • Did you try my suggestion? – Ted Lyngmo Mar 27 '21 at 09:24
  • Yes sir. Im still a newbie I can't understand the std::cout I'm sorry. – Bajas Mar 27 '21 at 09:26
  • Yes sir it works. However User must be free on inputting the lightyear number instead of fix data. – Bajas Mar 27 '21 at 09:34
  • @Bajas I added a demo to the answer where the input is taken from the input stream (`std::cin`). With some decoration: https://godbolt.org/z/s43jxhjs7 – Ted Lyngmo Mar 27 '21 at 09:34
  • Hi Sir It works!. THANK YOU VERYY MUCH. I've learned a lot from this. – Bajas Mar 27 '21 at 09:42