ssercpp12 / Homework

Homework result for 12 sser cpp
1 stars 8 forks source link

week 12 - 1001 #20

Closed YouseYeung closed 11 years ago

YouseYeung commented 11 years ago

cout << fixed << setpricision(2); if it is cout << setpricision(2),the answer will be wrong.Why?

omegaga commented 11 years ago

When you use cout << fixed << setprecision(2), it will print 2 decimal places. But when you do not use fixed, it will show 2 significant figures.

YouseYeung commented 11 years ago

灰常感谢师兄的回复。但我还不是很明白哈,是加fixed有四舍五入,不加就只是省略掉之后的数字的意思吗?

omegaga commented 11 years ago

try to run this code and you will be clear.

#include <iostream>
#include <iomanip>

int main(void) {
  double a = 123.45678;
  std::cout << std::setprecision(4) << a << std::endl;
  std::cout << std::fixed << std::setprecision(4) << a << std::endl;
}
YouseYeung commented 11 years ago

I got it! 3Q!