描述
它用於將 str 流的 floatfield 格式標誌設置為科學。當 floatfield 設置為科學時,浮點值使用科學記數法寫入:該值始終以小數點前僅一位數字表示,後跟小數點以及與精度字段(精度)一樣多的十進製數字。最後,這個符號總是包含一個 index 部分,由字母 e 後跟一個可選的符號和三個 index 數字組成。
C++98
浮點格式標誌既是選擇性標誌又是切換標誌:它可以采用以下一個或多個值,如下所示 -
標誌值 | 設置時的效果 |
---|---|
fixed | 以定點表示法寫入浮點值 |
scientific | 用科學記數法寫浮點值。 |
(沒有) | 以默認浮點表示法寫入浮點值。 |
C++11
浮點字段格式標誌既是選擇性標誌又是切換標誌:它可以采用以下任何值,也可以沒有,如下所示 -
標誌值 | 設置時的效果 |
---|---|
fixed | 以定點表示法寫入浮點值。 |
scientific | 用科學記數法寫浮點值。 |
hexfloat | 以十六進製格式寫入浮點值。 這個值是一樣的 |
defaultfloat | 以默認浮點表示法寫入浮點值。這是默認值(與無相同,在任何其他浮場位已設置)。 |
聲明
以下是 std::scientific 函數的聲明。
ios_base& scientific (ios_base& str);
參數
str− 格式標誌受到影響的流對象。
返回值
它返回參數 str。
異常
Basic guarantee- 如果拋出異常,則 str 處於有效狀態。
數據競爭
它修改了 str。對同一個流對象的並發訪問可能會導致數據競爭。
示例
在下麵的例子中解釋了 std::scientific 函數。
#include <iostream>
int main () {
double a = 3.1415926534;
double b = 2006.0;
double c = 1.0e-10;
std::cout.precision(5);
std::cout << "default:\n";
std::cout << a << '\n' << b << '\n' << c << '\n';
std::cout << '\n';
std::cout << "fixed:\n" << std::fixed;
std::cout << a << '\n' << b << '\n' << c << '\n';
std::cout << '\n';
std::cout << "scientific:\n" << std::scientific;
std::cout << a << '\n' << b << '\n' << c << '\n';
return 0;
}
讓我們編譯並運行上麵的程序,這將產生以下結果 -
default: 3.1416 2006 1e-010 fixed: 3.14159 2006.00000 0.00000 scientific: 3.14159e+000 2.00600e+003 1.00000e-010
相關用法
- C++ ios eof()用法及代碼示例
- C++ ios manipulators boolalpha()用法及代碼示例
- C++ ios manipulators left()用法及代碼示例
- C++ ios fixed用法及代碼示例
- C++ ios manipulators dec()用法及代碼示例
- C++ ios manipulators uppercase()用法及代碼示例
- C++ ios noshowbase用法及代碼示例
- C++ ios manipulators hex()用法及代碼示例
- C++ ios manipulators unitbuf()用法及代碼示例
- C++ ios showpos用法及代碼示例
- C++ ios manipulators nouppercase()用法及代碼示例
- C++ ios manipulators skipws()用法及代碼示例
- C++ ios dec用法及代碼示例
- C++ ios Internal用法及代碼示例
- C++ ios manipulators internal()用法及代碼示例
- C++ ios hex用法及代碼示例
- C++ ios manipulators fixed()用法及代碼示例
- C++ ios manipulators oct()用法及代碼示例
- C++ ios operator()用法及代碼示例
- C++ ios manipulators showbase()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ ios Library - Function Scientific。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。