Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

JotaKa

printf("%.2f); equivalent in C++

This topic is 5229 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

if i wanted to display a float to two decimal places in C++ using cout, how would I go about doing that?

Share this post


Link to post
Share on other sites
Advertisement
#include <iostream>

int main()
{
float f = 1.23456;
std::cout.precision(2);
std::cout << f << std::endl;
};
Or
#include <iostream>
#include <iomanip>

int main()
{
float f = 1.23456;
std::cout << std::setprecision(2) << f << std::endl;
};


Note - this is a sticky setting.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)

Share this post


Link to post
Share on other sites
Isn''t ''double'' 2 behind the point?

Technology can either save us or destroy us. Me ... I say it will destroy us if Microsoft is the example.

Share this post


Link to post
Share on other sites
quote:
Original post by Xeneth
Isn''t ''double'' 2 behind the point?

Technology can either save us or destroy us. Me ... I say it will destroy us if Microsoft is the example.


what? perhaps in kentucky it is.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!