c++ - C++ 点钞机中不显示小数

标签 c++

我写了一个 C++ 程序(应该是一个点钞机),我的代码有一些问题,我需要显示小数。如果重要的话,我使用 cout 而不是 printf

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main() {
    // Strings and Integers
    int dollars;
    int pennies;
    int nickles;
    int quarters;
    int halfDollars;
    int dimes;
    int fiveBill;
    int tenBill;
    int twentyBill;
    int fiftyBill;
    int hundredBill;

    // Coin/Bill Amounts
    int penny = 0.01;
    int dollar = 1.00;
    int nickle = 0.05;
    int quarter = 0.25;
    int halfDollar = 0.50;
    int dime = 0.10;
    int five = 5.00;
    int ten = 10.00;
    int twenty = 20.00;
    int fifty = 50.00;
    int hundred = 100.00;

    // Enter Amount
    cout << "Count your money!\n\n" << endl << "Hundred Dollar Bills: ";
    cin >> hundredBill;
    cout << "\nFifty Dollar Bills: ";
    cin >> fiftyBill;
    cout << "\nTwenty Dollar Bills: ";
    cin >> twentyBill;
    cout << "\nTen Dollar Bills: ";
    cin >> tenBill;
    cout << "\nFive Dollar Bills: ";
    cin >> fiveBill;
    cout << "\nOne Dollar Bills: ";
    cin >> dollars;
    cout << "\nHalf-Dollars: ";
    cin >> halfDollars;
    cout << "\nQuaters: ";
    cin >> quarters;
    cout << "\nDimes: ";
    cin >> dimes;
    cout << "\nNickles: ";
    cin >> nickles;
    cout << "\nPennies: ";
    cin >> pennies;

    // Add Together
    cout << (hundred * hundredBill) + (fifty * fiftyBill) + (twenty *    twentyBill)  + (ten * tenBill) + (five * fiveBill) + (dollars * dollar) +   (halfDollar * halfDollars) + (quarter * quarters) + (dime * dimes) + (nickle *     nickles)   + (penny * pennies);
    system("PAUSE");

    return 0;
}

最佳答案

您的问题:

int penny = 0.01;

penny 是一个int,名字是'integral value'的缩写。 0.01 是 double 类型。如果将 double (作为文字或来自另一个变量)分配给任何形式的 int(int、long int、short int、...),则仅分配整数部分并删除小数点(简单地删除,不四舍五入发生 - 无论该值与下一个更大的整数值有多接近)。

所以 penny 实际上只包含 0。与其他变量一样,dollar 是 1,nickle 也是 0,...

您现在有两个选择。或者,您将所有数字转换为 double ,或者您通过以美分分配所有值来做一些小技巧:

int penny = 1;
int dollar = 100;

这是我想要的。然后只有在输出时你才会做适当的格式化:

printf("the value of my variable is %d.%.2d $\n", value / 100, value % 100);

编辑:

由于许多人更喜欢通过 std::cout 输出,这变得相当麻烦,一种方便的方法如下:

class Formatter
{
    int value;
    friend std::ostream& operator<<(std::ostream& s, Formatter f);
public:
    Formatter(int value)
            : value(value)
    {
    }
};
typedef Formatter F, M;

std::ostream& operator<<(std::ostream& s, Formatter f)
{
    char c = s.fill();
    return s << f.value / 100 << '.'
        << std::setfill('0') << std::setw(2) << f.value % 100
        << std::setfill(c); // restore previous fill character!
}

当然,Typedef 不是必需的,只是为了说明其他名称——选择最适合您的名称(F:Formatter,M:Money,D:Dollar,...)。然后用法:

std::cout << F(penny) << std::endl;

关于c++ - C++ 点钞机中不显示小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38195894/

相关文章:

c++ - 迭代器操作问题

c++ - C++ 静态初始化在同一个回溯中出现两次是否正常?

c++ - 类模板仅对两种类型有用

c++ - QDialogs 中是否需要析构函数?

c++ - 为使用 Alchemy 转换的 C++ 类创建 AS3 代码(类似于 Box2D)

c++ - 将char数组初始化为字符串值,未初始化的索引是否设置为null?

c++ - cin.get();当我把它放在 if 语句中时不起作用

c++ - MAKE 如何知道要检查哪个源文件

c++ - 比较 std::vector 的指针来检查相等性是否安全?

c++ - "(void) new"在 C++ 中是什么意思?