當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ ios manipulators scientific()用法及代碼示例


C++中的流操縱器的scientific()方法用於為指定的str流設置floatfield格式標誌。此標誌將浮點數設置為科學。這意味著浮點值將以科學計數法表示。

用法:

ios_base& scientific (ios_base& str)

參數:此方法接受str作為參數,這是格式標誌受影響的流。


返回值:此方法返回設置了內部格式標誌的流str。

範例1:

// C++ code to demonstrate 
// the working of scientific() function 
  
#include <iostream> 
  
using namespace std; 
  
int main() 
{ 
  
    // Initializing the float values 
    double x = 1.23; 
  
    cout.precision(5); 
  
    cout << "without scientific flag:"
         << x << endl; 
  
    // Using scientific() 
    cout << "with scientific flag:"
         << scientific << x << endl; 
  
    return 0; 
}
輸出:
without scientific flag:1.23
with scientific flag:1.23000e+00

範例2:

// C++ code to demonstrate 
// the working of scientific() function 
  
#include <iostream> 
  
using namespace std; 
  
int main() 
{ 
  
    // Initializing the float values 
    double x = 1.0; 
  
    cout.precision(5); 
  
    cout << "without scientific flag:"
         << x << endl; 
  
    // Using scientific() 
    cout << "with scientific flag:"
         << scientific << x << endl; 
  
    return 0; 
}
輸出:
without scientific flag:1
with scientific flag:1.00000e+00

範例3:

// C++ code to demonstrate 
// the working of scientific() function 
  
#include <iostream> 
  
using namespace std; 
  
int main() 
{ 
  
    // Initializing the float values 
    double x = 1.23e9; 
  
    cout.precision(5); 
  
    cout << "without scientific flag:"
         << x << endl; 
  
    // Using scientific() 
    cout << "with scientific flag:"
         << scientific << x << endl; 
  
    return 0; 
}
輸出:
without scientific flag:1.23e+09
with scientific flag:1.23000e+09

參考: http://www.cplusplus.com/reference/ios/scientific/



相關用法


注:本文由純淨天空篩選整理自guptayashgupta53大神的英文原創作品 ios manipulators scientific() function in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。