如何四舍五入的总成本(双)到最接近的整数

How to round off the totalcost (double) to nearest inetegrs

本文关键字:最接近 整数 四舍五入 总成本      更新时间:2023-10-16

我试着在网上搜索类似的问题,但non对我有用。我得到的最接近的答案是"如果0.5之前的数字是奇数向上四舍五入,如果偶数向下四舍五入13.5是14,而12.5是12"。

回到问题:

我只需要用公式计算餐后的总量;

总餐费=餐费+餐费*小费% +餐费*税费%

我想出了这段代码(只是粗略的)

#include<iostream>
#include<math.h>
#include <iomanip>
using namespace std;
int main () {
    double mealCost;
    double total=0;
    double tipp,taxx=0;
    cin>>mealCost;
    int tip;
    cin>>tip;
    tipp=(tip*mealCost)/100;
    int tax;
    cin>>tax;
    taxx=(tax*mealCost)/100;
    total=mealCost+tipp+taxx;
    cout << fixed << showpoint;
    cout << setprecision(2);
    cout<<total<<endl;
    return 0;
}

,但输入值为10.75(mealamonut), 17(tip%),5 (tax %)。我得到的值是12.50如果我使用

int totalSum = std::round(total);

被转换成12但我的要求是13。如何实现?

如果存在,我找不到任何重复的问题请提及。

有多种方法可以将双精度型转换为整数。你有很多种子弹。点击这里,分别是std::round, std::lround, std::llround

另一方面,如果你想做的不是四舍五入,而是在一个方向上消除分数,那么你有std::ceil总是更高,std::floor总是更低。

记得包含<cmath>,而不是<math.h>。后者用于C,而不是C++

您可以通过使用std::ceil()和std::floor()来实现您的目标,这是在cmath头文件下定义的。

您总是尝试四舍五入,因此需要使用ceil()函数。Ceil是天花板的缩写,还有一个地板功能。天花板是向上的,地板是向下的,这里有一个c代码片段可以试用。

#include <stdio.h>      /* printf */
#include <math.h>       /* ceil */
int main ()
{
  printf ( "ceil of 2.3 is %.1fn", ceil(2.3) );
  printf ( "ceil of 3.8 is %.1fn", ceil(3.8) );
  printf ( "ceil of -2.3 is %.1fn", ceil(-2.3) );
  printf ( "ceil of -3.8 is %.1fn", ceil(-3.8) );
  return 0;
}

for舍入到最接近的整数math.h has nearbyint

 printf ( "nearbyint (2.3) = %.1fn", nearbyint(2.3) );
 printf ( "nearbyint (3.8) = %.1fn", nearbyint(3.8) );
输出:

nearbyint (2.3) = 2.0
nearbyint (3.8) = 4.0

或者如果你想打破。5

时的默认舍入行为
int totalSum= (total - floor(total) ) >= 0.5 ? ceil(total) : floor(total);

1) 10.75 + 17 * 10.75/100 + 5 * 10.75/100 = 13.115……我怎么拿不到12.50?

2)你怎么知道它是12.50,你怎么检查结果的值?(可能只有12.4999…请确保您检查了实际的真实值(理想情况下是在调试器中或以字节为单位转储内存内容并手动重建该值),而不是一些字符串格式的中间值。

3)这不是什么生产代码,对吗?在真正的财务软件中,金额不是用双倍来计算的,因为双倍不够准确,你会遇到各种各样的增值税四舍五入等难题。如果这是一些真实的事情,你不能胜任这项任务,请寻求专业人士的帮助。

答:

std::round通常应该做你需要的。如果以12结尾,则说明该值小于12.5。

如果四舍五入到小数点后两位,它显示为12.50,你就遇到了真正的金融软件中"各种各样的难题"之一。

然后您应该使用字符串表示创建自己的round,就像这个例子一样(不处理负数和可能重新发明轮子):

#include <iostream>
#include <string>
/**
 * Rounds floating point value in std::string type.
 * Works only for positive values, and without "+" sign!
 * ie. regexp ~d*.?d*~ formatting.
 * For malformed input the output is undefined (but should not crash).
 **/
std::string financialRound(const std::string & amount) {
    const size_t dotPos = amount.find_first_of('.');
    if (std::string::npos == dotPos) return amount; // no decimals found
    // copy whole part into temporary result
    std::string result = (0 < dotPos) ? amount.substr(0, dotPos) : "0";
    const size_t firstDecimalPos = dotPos + 1;
    // see if there is 5 to 9 digit after dot
    if (amount.size() <= firstDecimalPos) return result; // no char
    const char firstDecimal = amount.at(firstDecimalPos);
    if (firstDecimal < '5' || '9' < firstDecimal) return result; //not 5-9
    // add 1 to the result
    int patchPos = (int)result.size();
    while (0 <= --patchPos) {
        ++result.at(patchPos);
        if ('9'+1 == result.at(patchPos)) result.at(patchPos) = '0';
        else break;
    }
    // check if additional 1 is required (add overflow)
    if (-1 == patchPos) result.insert(result.begin(), '1');
    return result;
}
void tryValue(const std::string & amount) {
    printf(""%s" is rounded to "%s"n", amount.c_str(), financialRound(amount).c_str());
}
int main()
{
    printf("Trying normal values...n");
    tryValue("12.50");
    tryValue("12.49");
    tryValue(".49");
    tryValue(".50");
    tryValue("9.49");
    tryValue("9.50");
    printf("Missing decimals...n");
    tryValue("12");
    tryValue("12.");
    printf("Malformed...n");
    tryValue("");
    tryValue("a.4");
    tryValue("a.5");
    tryValue("12..");
}

cpp.sh