std::fixed, std::scientific, std::hexfloat, std::defaultfloat
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <ios> で定義
|
||
std::ios_base& fixed( std::ios_base& str ); |
(1) | |
std::ios_base& scientific( std::ios_base& str ); |
(2) | |
std::ios_base& hexfloat( std::ios_base& str ); |
(3) | (C++11以上) |
std::ios_base& defaultfloat( std::ios_base& str ); |
(4) | (C++11以上) |
浮動小数点の入出力に対するデフォルトの書式を変更します。
1)
str.setf(std::ios_base::fixed, std::ios_base::floatfield) を呼んだかのように、ストリーム str の floatfield を fixed に設定します。2)
str.setf(std::ios_base::scientific, std::ios_base::floatfield) を呼んだかのように、ストリーム str の floatfield を scientific に設定します。3)
str.setf(std::ios_base::fixed | std::ios_base::scientific, std::ios_base::floatfield) を呼んだかのように、ストリーム str の floatfield に fixed と scientific を同時に設定します。 これは16進浮動小数点フォーマットを有効化します。4)
str.unsetf(std::ios_base::floatfield) を呼んだかのように、ストリーム str の floatfield をゼロに設定します。 これはデフォルトの浮動小数点フォーマット (fixed とも scientific とも異なります) を有効化します。これは入出力マニピュレータであり、 std::basic_ostream 型の任意の out に対する out << std::fixed のような式や std::basic_istream 型の任意の in に対する in >> std::scientific のような式で呼ぶことができます。
引数
| str | - | 入出力ストリームへの参照 |
戻り値
str (操作後のストリームへの参照)。
ノート
16進浮動小数点フォーマットは、 std::num_put::do_put の仕様によって要求されている、ストリームの精度の仕様を無視します。
例
Run this code
#include <iostream>
#include <sstream>
void print(const char* textNum, double num)
{
std::cout << "The number " << textNum << " in fixed: " << std::fixed << num << '\n'
<< "The number " << textNum << " in scientific: " << std::scientific << num << '\n'
<< "The number " << textNum << " in hexfloat: " << std::hexfloat << num << '\n'
<< "The number " << textNum << " in default: " << std::defaultfloat << num << '\n';
}
int main()
{
print("0.01 ", 0.01);
print("0.00001", 0.00001);
double f;
std::istringstream("0x1P-1022") >> std::hexfloat >> f;
std::cout << "Parsing 0x1P-1022 as hex gives " << f << '\n';
}
出力:
The number 0.01 in fixed: 0.010000
The number 0.01 in scientific: 1.000000e-02
The number 0.01 in hexfloat: 0x1.47ae147ae147bp-7
The number 0.01 in default: 0.01
The number 0.00001 in fixed: 0.000010
The number 0.00001 in scientific: 1.000000e-05
The number 0.00001 in hexfloat: 0x1.4f8b588e368f1p-17
The number 0.00001 in default: 1e-05
Parsing 0x1P-1022 as hex gives 2.22507e-308
関連項目
| 浮動小数点の精度を変更します (関数) |