简体   繁体   中英

C++ Float Precision is incorrect

I'm new to programming and I'm experimenting with very basic programs. I just wrote a program to convert USD to GBP. When I run the program I don't get the exact GBP value. For example if I enter 5 USD the program returns 3.25 GBP. However, the correct value should be 3.23. Here is the code. Can/will someone tell me what I'm doing wrong? Please.

#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;

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM