Sign in to follow this  
jordi_0071

variable wich on?

Recommended Posts

hey i write this code: #include <iostream> using namespace std; int main() { float sum; int ten = 10; float pi = 3.10; sum = pi + ten; cout << "The sum as whole number: " << sum << endl; return 0; } the anwnser is 13.10 but it shows 13.1! how do i show 13.10?

Share this post


Link to post
Share on other sites
Formatted output using the functions of C would work.

#include <stdio.h>

int main()
{
float number = 3.45;

printf( "%.5f and %f", number, number );

return 0;
}

// Output: 3.45000 and 3.45

Share this post


Link to post
Share on other sites
Or, the C++ way using cout: (EDIT: just realised, moeron actually pointed it out)

#include <iostream>
#include <iomanip> //Used for setprecision

int main()
{
const float myFloat = 13.1f;
std::cout << std::fixed << std::setprecision(2) << myFloat << std::endl;

return 0;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this