如何在计算器中将大数字表示为小数点后两位C++
How to express large numbers to two decimal places in C++ Calculator
我正在尝试用C++编写一个计算器,该计算器执行/、*、-或 + 的基本功能,并显示两个小数位的答案(精度为 0.01)。
例如100.1 * 100.1
应该将结果打印为10020.01
但我得到-4e-171
.据我了解,这是来自溢出,但这就是我首先选择long double
的原因!
#include <iostream>
#include <iomanip>
using namespace std;
long double getUserInput()
{
cout << "Please enter a number: n";
long double x;
cin >> x;
return x;
}
char getMathematicalOperation()
{
cout << "Please enter which operator you want "
"(add +, subtract -, multiply *, or divide /): n";
char o;
cin >> o;
return o;
}
long double calculateResult(long double nX, char o, long double nY)
{
// note: we use the == operator to compare two values to see if they are equal
// we need to use if statements here because there's no direct way
// to convert chOperation into the appropriate operator
if (o == '+') // if user chose addition
return nX + nY; // execute this line
if (o == '-') // if user chose subtraction
return nX - nY; // execute this line
if (o == '*') // if user chose multiplication
return nX * nY; // execute this line
if (o == '/') // if user chose division
return nX / nY; // execute this line
return -1; // default "error" value in case user passed in an invalid chOperation
}
void printResult(long double x)
{
cout << "The answer is: " << setprecision(0.01) << x << "n";
}
long double calc()
{
// Get first number from user
long double nInput1 = getUserInput();
// Get mathematical operations from user
char o = getMathematicalOperation();
// Get second number from user
long double nInput2 = getUserInput();
// Calculate result and store in temporary variable (for readability/debug-ability)
long double nResult = calculateResult(nInput1, o, nInput2);
// Print result
printResult(nResult);
return 0;
}
setprecision
告诉它你想要多少位小数作为int
,所以你实际上是将其设置为setprecision(0)
,因为0.01
被截断了。在您的情况下,您希望将其设置为 2。你也应该使用std::fixed
否则你会得到科学数字。
void printResult(long double x)
{
cout << "The answer is: " << std::fixed << setprecision(2) << x << "n";
}
工作示例
这不是由于溢出,你会得到奇怪的结果。双精度可以轻松地将数字保持在您所显示的范围内。
尝试在没有设置精度的情况下打印结果。
编辑:尝试后
long double x = 100.1;
cout << x << endl;
我看到它在我的 Windows 系统上不起作用。
所以我搜索了一下,发现:
在窗口上打印长双精度
也许这就是解释。
所以我尝试了
long double x = 100.1;
cout << (double)x << endl;
效果很好。
第二次编辑:
另请参阅拉斐尔提供的此链接
http://oldwiki.mingw.org/index.php/long%20double
默认浮点表示在314.15
和3.1e2
等表示之间自动切换,具体取决于数字的大小和可以使用的最大位数。在此演示文稿中,精度是最大位数。默认情况下为 6。
您可以增加最大位数,以便您的结果可以像314.15
一样呈现,也可以通过使用std::fixed
操纵器强制使用这种定点表示法。对于std::fixed
,精度是小数位数。
但是,对于std::fixed
非常大和非常小的数字可能非常难以阅读。
setprecision()
操纵器指定小数点后的位数。 因此,如果要打印100.01
,请使用 setprecision(2)
.
使用 setprecision(0.01)
时,值 0.01
正在转换为 int
,其值为 0
。
如果您实际上阅读了setprecision()
的文档,那不会有什么坏处 - 它清楚地指定了一个int
参数,而不是浮点参数。
- 用c++把小数点转换成八进制
- 如何在 c++ 中将小数点后两位数的浮点数分配给另一个浮点数
- 使用 cin 时接受小数点后的 2 位数字
- 使用 stod() 转换小数点后字母的字符串时没有例外
- 如何在C++中只打印小数点而不设置限制,但动态设置精度
- 为什么在C++算术函数中加".0"会给出小数点的答案?
- C++ cout 列表,其中小数点使用 setw(x) 对齐,而不是put_money
- C++小数点后有 200 位数字的类型?
- 如何输出计算到小数点后两位的中间器?
- 如何在C或C 的小数点后两个位置后四舍五入
- 在C 中显示小数点,计算问题
- 所有测试用例都将小数点转换为二进制数
- 在函数定义中使用特征库时,小数点不会按预期存储
- 如何在C ++中打印特定位数的位数?例如,总共打印8位数字(小数点之前+之后的组合)
- 有人可以解释为什么这个程序可以计算小数点后的位数
- 小数点后提取值
- 如何在 C++ 中编写带有小数点的浮点值
- C++ 在数字开头添加小数点的任意方法
- 在 c++ 中显示小数点后五位数字,但如果小数点后有 0,请不要打印它们
- 将数字设置在小数点后的浮子中