gpt4 book ai didi

c++ - 从 Math C++ 获取美分

转载 作者:太空宇宙 更新时间:2023-11-04 16:10:50 24 4
gpt4 key购买 nike

现在我的代码可以正常工作,当我打印出他的美分时,我差了一个,因为当你说 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com