c++ - 在 C++ 中乘以两个 float 给我非数字结果

标签 c++

我是 C++ 新手,我的第一个程序有问题。我正在尝试将两个 float 相乘,结果总是显示为 1.1111e+1,其中 1s 是随机数。下面是我写的小程序。

#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
int main()
{
  float bank;
  float dollor;
  cout<<"Enter amount of $ deposited at Bank: ";//the data to input is 5000
  cin>>bank;
  cout<<"Enter current $ price: ";//1usd = 800mmk: the data to input is 800
  cin>>dollor;
  bank*=dollor;//all deposited $ to local currency
  cout<<"Result is "<<bank;
  getch();
}

这个程序的结果是 4e+006。

ps: 我声明为 float 以便在某个时候输入 float。 请帮我解决这个程序我错了的地方。谢谢大家..

最佳答案

4e+006scientific notation 4000000,这是 5000*800 的正确答案。

具体来说,4e+006代表4 * 10**6,其中10**6是十的六次方.

要使用定点表示法,您可以像这样更改您的程序:

#include <iomanip>
...
cout << "Result is " << fixed << bank;

关于c++ - 在 C++ 中乘以两个 float 给我非数字结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8534936/

相关文章:

c++ - fopen() 在打开数百次后无法正常工作是否有原因?

c++ - 如何添加一个字符数组(并使它们变成整数)

c++:以下代码崩溃

c++ - 从 RichEdit 复制编号列表?

C++ - 未解析的外部符号

c++ - 排序给出最后位置元素的错误输出

c++ - 模板参数推导 : which compiler is right here?

python - opencv.groupRectangles() c++ 和 python 层之间的差异

c++ - 有没有办法嵌入Windows保存对话框

c++ - OpenGL 调整纹理/四边形的大小