2016-09-06 36 views
-2

我試圖在網上搜索類似的問題,但非對我有用。 離我最近的是「如果.5之前的數字是奇數,如果連13.5變成14變成12.5變成12」。如何將總成本(雙倍)四捨五入到最近的inetegrs

編輯的問題:

我只需要與式飯後來計算總金額;

總量= mealamount + mealamount *尖端%+ mealamount *稅%

我想出了這一段代碼(粗只)

#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(小費%),5(稅率%)。 價值正在逐漸爲12.50 如果我使用

int totalSum = std::round(total); 

其得到轉化爲12 但我的要求是13。 如何實現這一目標?

我不能找到任何重複的問題,如果存在 請提。

+0

'std..floor( )','std :: ceil()' –

+0

πάνταῥεῖ我不能這樣做,在運行時我怎麼知道val得到了什麼,這段代碼只會失敗,而我提供的vals集對我來說還不錯。 –

回答

0

你可以使用std::ceil()std::floor()來實現你的目標,它是在cmath頭文件下定義的。

+0

我不能這樣做,在運行時,我怎麼知道我得到的值是多少,這段代碼只能通過vals集合失敗,我已經爲我提供了其他的罰款 –

+0

@ N.Nihar:如果你做了你的計算,精確。你可以達到你的目標。 10.75中的17%是1.8275,10.75中的5%是0.5375,因此總量變爲= 13.115。現在,如果你發言(13.115),那麼它將返回13. – Shravan40

+0

你能指出我的代碼有什麼問題,那就是我要找的 total = mealCost + tipp + taxx;輸出可以是任何東西,我的代碼只是在特定的測試用例上失敗,也就是我提到的集合,所以一般我不能去一個小區或樓層 –

4

有多種方法可以將雙打轉換爲整數。你有多種輪。檢查他們here,這是std::round, std::lround, std::llround。另一方面,如果你想要做的並不是四捨五入,而是將分數消除爲一個方向,那麼總是有更高的std::ceil,而總是更低的std::floor

記得包括<cmath>而不是<math.h>。後者用於C,而不是C++

+0

細胞和地板絕對不適用於我,但感謝您的解決方案。 std :: lround爲我做了詭計 –

+0

@ N.Nihar很高興能有所幫助。請選擇綠色複選框將問題標記爲已回答。 –

+0

你的意思是接受正確的答案!我找不到像綠色複選框 –

0

您試圖總是四捨五入,因此您需要使用ceil()函數。 Ceil是天花板的簡稱,也有地板功能。天花板升起,地板倒塌,這裏是一個可以試用的c片段。

#include <stdio.h>  /* printf */ 
#include <math.h>  /* ceil */ 

int main() 
{ 
    printf ("ceil of 2.3 is %.1f\n", ceil(2.3)); 
    printf ("ceil of 3.8 is %.1f\n", ceil(3.8)); 
    printf ("ceil of -2.3 is %.1f\n", ceil(-2.3)); 
    printf ("ceil of -3.8 is %.1f\n", ceil(-3.8)); 
    return 0; 
} 
0

四捨五入到最接近的整數math.h中有nearbyint

printf ("nearbyint (2.3) = %.1f\n", nearbyint(2.3)); 
printf ("nearbyint (3.8) = %.1f\n", nearbyint(3.8)); 

輸出:

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

或者,如果你想打破默認的舍入行爲時0.5

int totalSum= (total - floor(total)) >= 0.5 ? ceil(total) : floor(total); 
+0

ceil和d floor我不能使用,因爲我的輸出是在運行時決定的。 來到nearbyint(2.5)產生2作爲輸出 我需要的是3 我的問題是貨幣,所以根據要求12.5必須轉換爲13而不是12。 –

+0

我添加了一些可能有用的東西。根據數字與floor()版本之間的差異,然後根據結果 –

0

1)10.75 + 17 * 10.75/100 + 5 * 10.75/100 = 13.115 ...我怎麼得不到12.50?

2)你怎麼知道它是12.50,你如何檢查結果的價值? (可能只有12.4999 ...,所以當它被格式化到小數點後兩位時,它將變成12。50)確保你確實檢查了實際的實際值(理想情況下在調試器或轉儲內存中以字節爲單位並手動重構值),而不是某些字符串格式化的中間值。

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.."); 
} 

live demo上cpp.sh

+0

@ Ped7g thx動態地選擇floor()或ceil()作爲洞察 –

+0

@ N.Nihar:我正在回答在codereview中有點類似的東西,但我描述了更常見的解決方案:http://codereview.stackexchange.com/a/140851/110284 – Ped7g