在c++中货币计数器中没有显示小数
Decimals not showing up in money counter in 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!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;
如前所述,问题是您试图将十进制值赋给整数变量。
发生的情况是,您的输入(在十进制值的情况下)可以被编译器解释为double
或float
类型的变量。然而,在给定输入的赋值过程中,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'数据类型。
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 字符串-C++后显示的随机字符
- 继承期间显示未知行为的子类
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- C++将浮点指针值舍入为小数位数
- 如何设置数字格式以显示C 中的所有小数位数
- C 圆形为1个小数,显示2个小数
- STD :: SetPrecision不显示尾随的小数
- 我如何使其显示小数
- C++ - 为什么我的cout代码显示小数不一致
- 关于以二进制存储并显示其小数
- C++不显示小数位数的数字
- 显示带小数位数的数字,而不是整数
- 使用Float或Double类型时不显示小数
- 小数未正确显示 C++
- 如何在 8.th 小数之后擦除/不显示浮点小数
- 从二进制文件读取双精度(到类中)-小数不显示
- 在小数后显示有限的位数
- 在c++中货币计数器中没有显示小数