C++ setprecision(2) 打印一位小数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16618912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ setprecision(2) printing one decimal?
提问by lloyd
I'm trying to do some simple output in formatted text. Setprecision is not printing my variables out to two decimal places.
我正在尝试以格式化文本进行一些简单的输出。Setprecision 不会将我的变量打印到小数点后两位。
For example if firstItemPrice = 2.20, the output is 2.2 instead of 2.20
例如,如果 firstItemPrice = 2.20,则输出为 2.2 而不是 2.20
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
string firstitem = "";
string seconditem = "";
double firstItemNum;
double firstItemPrice = 0.00;
double secondItemNum;
double secondItemPrice = 0.00;
//first item
cout << "Enter the name of Item 1: ";
getline(cin, firstitem);
cout << "Enter the number of " << firstitem << "s and the price of each: ";
cin >> firstItemNum >> firstItemPrice;
cin.ignore();
//second item
cout << "Enter the name of Item 2: ";
getline(cin, seconditem);
cout << "Enter the number of " << seconditem << "s and the price of each: ";
cin >> secondItemNum >> secondItemPrice;
cout << left << setw(20) << "Item" << setw(10) << "Count"
<< setw(10) << "Price" << left << "\n";
cout << setw(20) << "====" << setw(10) << "====" << setw(10)
<< "====" << left << "\n";
cout << setw(20) << firstitem << setw(10)
<< firstItemNum << setw(10) << setprecision(2)
<< firstItemPrice << "\n";
cout << setw(20) << seconditem << setw(10) << secondItemNum
<< setprecision(2) << secondItemPrice << left << "\n";
return 0;
}
回答by Carl Norum
You need a fixedin there to do that.
你需要一个fixed在那里做到这一点。
cout << fixed;
Set it back using:
使用以下方法将其重新设置:
cout.unsetf(ios_base::floatfield);
In your case, changing the last bit of your program like this example should do it:
在你的情况下,像这个例子一样更改程序的最后一点应该这样做:
cout << setw(20) << firstitem << setw(10)
<< firstItemNum << setw(10) << fixed << setprecision(2)
<< firstItemPrice << "\n";
cout.unsetf(ios_base::floatfield);
cout << setw(20) << seconditem << setw(10) << secondItemNum
<< fixed << setprecision(2) << secondItemPrice << left << "\n";
Editorial aside: Don't use floating point numbers to represent currency values.
编辑旁白:不要使用浮点数来表示货币价值。
回答by Mooing Duck
from http://www.cplusplus.com/reference/ios/ios_base/precision/
来自http://www.cplusplus.com/reference/ios/ios_base/precision/
The floating-point precision determines the maximum number of digits to be writtenon insertion operations to express floating-point values. How this is interpreted depends on whether the floatfield format flag is set to a specific notation (either fixed or scientific) or it is unset (using the default notation, which is not necessarily equivalent to either fixed nor scientific).
For the default locale: Using the default floating-point notation, the precision field specifies the maximum number of meaningful digits to display in total counting both those before and those after the decimal point. Notice that it is not a minimum, and therefore it does not pad the displayed number with trailing zeros if the number can be displayed with less digits than the precision. In both the fixed and scientific notations, the precision field specifies exactly how many digits to display after the decimal point, even if this includes trailing decimal zeros. The digits before the decimal point are not relevant for the precision in this case.
浮点精度决定了在插入操作中写入以表达浮点值的最大位数。如何解释这取决于 floatfield 格式标志是设置为特定符号(固定或科学)还是未设置(使用默认符号,这不一定等同于固定或科学)。
对于默认语言环境: 使用默认浮点表示法,精度字段指定要显示的有意义数字的最大数量,包括小数点之前和之后的数字。请注意,它不是最小值,因此如果数字可以显示的位数少于精度,则它不会用尾随零填充显示的数字。在固定记数法和科学记数法中,精度字段准确指定小数点后显示的位数,即使这包括尾随小数零。在这种情况下,小数点前的数字与精度无关。

