• Advertisement
Sign in to follow this  

variable wich on?

This topic is 4711 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
check MSDN for the setprecision function. I believe that sets how many decimal points to show.

hth
moe.ron

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
Sign in to follow this  

  • Advertisement