簡體   English   中英

如何為代碼塊中的所有 cout 設置精度?

[英]How to set precision for all cout in the code block?

我不想重復使用 setprecision 並固定在 cout 上。 那么有什么方法可以只設置一個並將其用於代碼塊中的所有 cout 嗎?

int main()
{
  double num1 = 1.00000, num2 = 2.00005;
  cout **<< setprecision(5) << fixed <<** num1 << endl; <-- I do not to repeatedly set this.
  cout **<< setprecision(5) << fixed** << num2 << endl;
}

我在下面嘗試過,但它不起作用

int main()
{
   double num1 = 1.00000, num2 = 2.00005;
   cout.precision(5); cout.fixed; cout.showpoint; <-- Does not work
   cout << num1 << endl << num2 << endl;
}

任何建議表示贊賞。

舉個例子:

#include <iostream>
int main () {
double f = 3.14159;
std::cout.unsetf ( std::ios::floatfield );                
std::cout.precision(5);
std::cout << f <<endl;
std::cout.precision(10);
std::cout << f <<endl;
std::cout.setf( std::ios::fixed, std:: ios::floatfield ); 
std::cout << f <<endl;
return 0;
}

現在這給了我們一個 output :

3.1416

3.14159

3.1415900000

現在,在您的情況下,代碼應如下所示:

#include <iostream>
int main () {
double num1 = 1.00000, num2 = 2.00005;
std::cout.unsetf ;                
std::cout.precision(5);
std::cout << num1 <<endl;
std::cout << num2 <<endl;
return 0;
}

如果上述方法不起作用,我認為您將不得不重復並再次定義精度。

#include <iostream>
int main () {
double num1 = 1.00000, num2 = 2.00005;
std::cout.unsetf ;                
std::cout.precision(5);
std::cout << num1 <<endl;
std::cout.precision(5);
std::cout << num2 <<endl;
return 0;
}

There is a caveat that setting the .precision .precision() and the stream flags std::fixed and std::showpoint , etc.. will modify the stream flags as indicated by @dxiv in his now deleted answer and modify the stream behavior program-寬的。 這很重要,並且無論它們設置在哪里,都會對整個程序產生影響。

通常,您想出於有限的目的更改格式,例如對於<<的 class 重載中的所有浮點 output 或標題所示的“代碼塊”等...如果您在重載成員 function,它會影響整個 rest 程序中的std::cout - 這可能不是故意的。

C++ provides a simple mechanism to make the change on a temporary basis by saving the stream .flags() before adjusting the stream and then when done, restoring the original flags before you leave the scope where the changes were desired. 這將撤消std::ios_base::fmtflags ,例如std::fixedstd::showpoint設置,但必須通過恢復默認值6來撤消 .precision .precision()更改。

要在進行更改之前保存 stream 標志,您可以使用:

    std::ios_base::fmtflags f = std::cout.flags();      /* save format flags */

完成需要更改的 output 后,您可以使用以下命令恢復標志:

    std::cout.flags(f);                                 /* restore saved flags */

恢復默認.precision()需要將精度設置回默認值6 ,例如

    std::cout.precision(6);                             /* restore precision */

有兩種(實際上是三種)設置格式標志的方法。 (1) 您可以單獨使用,例如std::fixedstd::showpoint等,或者 (2,3) 您可以直接使用std::ios_base::setf或通過聲明 BitmaskType std::ios_base來操作標志::fmtflags 要直接設置std::fixedstd::showpoint標志,您可以執行以下操作:

std::cout.setf (std::ios_base::fixed, std::ios_base::showpoint);

要使用 BitmaskType,您將創建該類型的一個實例,並通過 OR'ing 和 AND'ing 值來設置所需標志的值。 然后使用.flags()成員 function 設置標志值,以與傳遞保存的格式標志相同的方式使用原始標志 Z9ED39E2EA93158EF5769A 的已保存 bitmap 所做的更改。 上面的std::ios_base::fmtflags鏈接提供了每種方式的示例。

在更改之前保存,然后在完成后恢復標志會重置標志,無論您使用哪種方法設置它們。

這是為代碼塊設置精度和固定標志的簡單方法:

const std::streamsize oldPrecision = std::cout.precision(8);
const std::ios::fmtflags oldFlags = std::cout.setf(std::ios::fixed);

// code here which requires std::cout to print with precision=8 in fixed format

std::cout.precision(oldPrecision);
std::cout.setf(oldFlags);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM