C ++ Float Precision不正确

时间:2015-07-14 02:53:17

标签: c++ precision

我是编程新手,我正在尝试非常基本的程序。我刚写了一个程序将USD兑换成GBP。当我运行程序时,我没有得到确切的GBP值。例如,如果我输入5美元,程序将返回3.25英镑。但是,正确的值应为3.23。这是代码。可以/有人会告诉我我做错了什么吗?请。

#include <iostream>
#include <cmath>
using namespace std;

float dtp(float);

int main()
{
float dollar;

cout <<"Enter the dollar amount you want converted to Great Britain     
Pounds: ";
cin >> dollar;
float pound = dtp(dollar);
    if (pound <= 1)
    {
    cout <<"The dollar amount you entered of " << dollar <<" dollar is                       
equal to " << pound <<" pound.";
    }

    else
    {
    cout <<"The dollar amount you entered of " << dollar <<" dollars is        
equal to " << pound <<" pounds.";
    }

return 0;
}

float dtp(float p)
{
return p * .65;

}

1 个答案:

答案 0 :(得分:1)

根据我的计算器,5 * .65 = 3.25,这就是你得到的答案。