How I print 2 decimal position after point in c++.

such as a=1.765, when I want to print a the number is a=1.76

asked 10 Jun, 12:08

undefined_riad's gravatar image

undefined_riad
915


There is a function called setprecition in C++

Code:

// setprecision example
#include <iostream>     // std::cout, std::fixed
#include <iomanip>      // std::setprecision

int main () {
  double f =3.14159;
  std::cout << std::setprecision(5) << f << '\n';
  std::cout << std::setprecision(9) << f << '\n';
  std::cout << std::fixed;
  std::cout << std::setprecision(5) << f << '\n';
  std::cout << std::setprecision(9) << f << '\n';
  return 0;
}

Output: 
3.1416
3.14159
3.14159
3.141590000

Thanks :)

permanent link

answered 10 Jun, 13:40

menon's gravatar image

menon
3.3k220

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×104
×2

question asked: 10 Jun, 12:08

question was seen: 193 times

last updated: 10 Jun, 13:40