在c++中货币计数器中没有显示小数

Decimals not showing up in money counter in C++

本文关键字:显示 小数 计数器 c++ 货币      更新时间:2023-10-16

我写了一个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!nn" << 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为整型,是整数值的缩写。0.01是double类型。如果给任何形式的int (int, long int, short int,…)赋值双精度型(无论是作为字面量还是来自另一个变量),只赋值整型部分,并且去掉小数点(简单地去掉,不进行舍入——无论该值与下一个更大的整型值有多接近)。

所以penny实际上只持有0。与其他变量一样,dollar为1,nickle为0,…

你现在有两个选择。要么,你把所有的数字都转换成双精度,要么你做一个小技巧,把所有的值都以分为单位赋值:

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;

如前所述,问题是您试图将十进制值赋给整数变量。

发生的情况是,您的输入(在十进制值的情况下)可以被编译器解释为doublefloat类型的变量。然而,在给定输入的赋值过程中,int或整型(一个整数)只能保存一个没有小数部分的值。编译器会注意到这一点,并简单地将给定的输入缩小为int变量可以保存的值。编译器对小数点之后的内容不感兴趣,并直接丢弃其余部分。

,

int a = 3.5 // int can't hold the decimal 0.5, narrows into 3
int b = 3 // int can hold this, no narrowing
double d = 3.5 // double can hold this, no narrowing
float f = 3.5 // float can hold this, no narrowing

一个好的方法是将所有的整型变量替换为double类型。在这个简单的程序中,您不需要使用printf来格式化输入。

在你想知道的情况下,为什么我要使用double而不是float

下面是一些附加信息:

https://softwareengineering.stackexchange.com/questions/188721/when-do-you-use-float-and-when-do-you-use-double

我应该使用double还是float?

如果要保持整数,则将结果强制转换为float或double类型。然后将精度设置为2位数和固定格式。

#include <iostream>
#include <iomanip>
...
float total = (float) ((hundred * hundredBill) + (fifty * fiftyBill) + (twenty *    twentyBill) + (ten * tenBill) + (five * fiveBill) + (dollars * dollar) + (halfDollar * halfDollars) + (quarter * quarters) + (dime * dimes) + (nickle * nickles) + (penny * pennies));
cout << std::setprecision(2) << std::fixed << total << endl;

如果需要的话,我用"cout"代替"printf"

不,不管你想要什么输出都没关系。

使用所有想要操作wrt小数的变量作为'double'数据类型。