现在我的代码可以正常工作,当我打印出他的美分时,我差了一个,因为当你说 0.95 * 100 时编译器会出现舍入错误,它会给我 94 或不是 95 的东西。我知道数学是如何正常计算的,但是由于这个舍入误差我做不到。
也别担心问我为什么同时使用C和C++。这是因为这就是我的导师想要的。
这是相关代码。
if (dollarAmount < 10.00 && dollarAmount >= 1.00)
{
cents = (dollarAmount - static_cast<int>(dollarAmount)) * 100;
digit = (((static_cast<int>(dollarAmount)/ 1)));
strcat(amountInWords, lookUp[digit]);
strcat(amountInWords, " dollars and ");
}
整个项目代码:https://gist.github.com/anonymous/65c052277c67f03de643
最佳答案
当舍入很重要时,不要使用浮点变量。 只需使用一个 long 变量来存储美分。
关于c++ - 从 Math C++ 获取美分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28865627/