名前空間
変種
操作

std::fixed, std::scientific, std::hexfloat, std::defaultfloat

提供: cppreference.com
< cpp‎ | io‎ | manip
 
 
入出力ライブラリ
入出力マニピュレータ
Cスタイルの入出力
バッファ
(C++98で非推奨)
ストリーム
抽象
ファイル入出力
文字列入出力
配列入出力
(C++98で非推奨)
(C++98で非推奨)
(C++98で非推奨)
同期化出力
エラーカテゴリインタフェース
(C++11)
 
入出力マニピュレータ
浮動小数点フォーマット
fixedscientifichexfloatdefaultfloat
(C++11)(C++11)
整数フォーマット
ブーリアンフォーマット
フィールド幅とフィル制御
その他のフォーマット
ホワイトスペース処理
出力のフラッシュ
ステータスフラグ操作
時間と通貨の入出力
(C++11)
(C++11)
(C++11)
(C++11)
引用符マニピュレータ
(C++14)
 
ヘッダ <ios> で定義
(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) を呼んだかのように、ストリーム strfloatfieldfixed に設定します。
2) str.setf(std::ios_base::scientific, std::ios_base::floatfield) を呼んだかのように、ストリーム strfloatfieldscientific に設定します。
3) str.setf(std::ios_base::fixed | std::ios_base::scientific, std::ios_base::floatfield) を呼んだかのように、ストリーム strfloatfieldfixedscientific を同時に設定します。 これは16進浮動小数点フォーマットを有効化します。
4) str.unsetf(std::ios_base::floatfield) を呼んだかのように、ストリーム strfloatfield をゼロに設定します。 これはデフォルトの浮動小数点フォーマット (fixed とも scientific とも異なります) を有効化します。

これは入出力マニピュレータであり、 std::basic_ostream 型の任意の out に対する out << std::fixed のような式や std::basic_istream 型の任意の in に対する in >> std::scientific のような式で呼ぶことができます。

目次

[編集] 引数

str - 入出力ストリームへの参照

[編集] 戻り値

str (操作後のストリームへの参照)。

[編集] ノート

16進浮動小数点フォーマットは、 std::num_put::do_put の��様によって要求されている、ストリームの精度の仕様を無視します。

[編集]

#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

[編集] 関連項目

浮動小数点の精度を変更します
(関数) [edit]