0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

C++桁数設定

Posted at

C++桁数設定

std::fixedとstd::setprecision組み合わせで設定できる。

std::setprecision

サンプル

std::ostringstream oss;
double num = 3.14159265359;
double num2 = 12.3456789;
oss << std::setprecision(5) << num; 
std::cout << oss.str() << std::endl;

std::cout << std::endl;

oss.str("");
oss << std::setprecision(5) << num2; 
std::cout << oss.str() << std::endl;

出力結果(4桁と3桁)

3.1416

12.346

std::fixedとstd::setprecision組み合わせ

std::ostringstream oss;
double num = 3.14159265359;
double num2 = 12.3456789;
oss << std::fixed << std::setprecision(5) << num;
std::cout << oss.str() << std::endl;

std::cout << std::endl;

oss.str("");
oss << std::fixed << std::setprecision(5) << num2;
std::cout << oss.str() << std::endl;

出力結果(5桁)

3.14159

12.34568
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?