回答編集履歴

3

確認環境の追記

2025/05/31 11:14

投稿

hiroki-o
hiroki-o

スコア1416

test CHANGED
@@ -1,3 +1,10 @@
1
+ 以下の3環境で確認しました。
2
+ |OS|コンパイラー|
3
+ |-|-|
4
+ |Windows 11|Visual Studio 2022|
5
+ |FreeBSD 14.2|GCC 15.0.1|
6
+ |FreeBSD 14.2|Clang 21.0.0|
7
+
1
8
  `std::fixed << std::setprecision(n)`で小数点以下の桁数nを指定します。
2
9
  ```
3
10
  #include <iomanip>

2

回答の追記

2025/05/31 07:37

投稿

hiroki-o
hiroki-o

スコア1416

test CHANGED
@@ -11,3 +11,21 @@
11
11
  v=(1.0,2.0)
12
12
  vの長さ2.2
13
13
  ```
14
+ (追記)
15
+ コメントにあるように、C++20以降なら`std::format`が使えます。
16
+ ```
17
+ #include <format>
18
+ (中略)
19
+ //cout << "v=(" << v.x << "," << v.y << ")" << endl;
20
+ cout << format("v=({:.1f},{:.1f})", v.x, v.y) << endl;
21
+ ```
22
+ 出力
23
+ ```
24
+ v.x=1.0
25
+ v.y=2.0
26
+ v=(1.0,2.0)
27
+ vの長さ2.23607
28
+ ```
29
+
30
+ フォーマット指定の詳細は、以下を参照してください。
31
+ [https://cpprefjp.github.io/reference/format/format.html](https://cpprefjp.github.io/reference/format/format.html)

1

回答の修正

2025/05/25 04:51

投稿

hiroki-o
hiroki-o

スコア1416

test CHANGED
@@ -1,5 +1,7 @@
1
1
  `std::fixed << std::setprecision(n)`で小数点以下の桁数nを指定します。
2
2
  ```
3
+ #include <iomanip>
4
+ (中略)
3
5
  cout << fixed << setprecision(1);
4
6
  ```
5
7
  出力