您可以根據avg
值的浮動模數來選擇精度。以下作品:
int main() {
double sum = 10;
double count = 3;
double avg = sum/count;
double mod;
std::cout << std::fixed
<< std::setprecision((modf(avg, &mod) != 0.0) ? 2 : 0)
<< avg
<< std::endl;
}
考慮補充規格:
- 寫2.5,而不是2.50
- 撰寫3.14 3.1421783921,而不是3.15
下面是一個使用可能實現@ IInspectable建議的方法:
std::stringstream ss;
ss << std::fixed << avg;
size_t pos = ss.str().find('.');
if (pos + 2 < ss.str().size()) // If there is a '.' and more than 2 digits after it
pos += 3;
std::string s = ss.str().substr(0, pos); // Leave only two digits
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { return ch != '0'; }).base(), s.end()); // Remove trailing zeros
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { return ch != '.'; }).base(), s.end()); // Remove trailing '.' when needed
std::cout << s << std::endl;
這將輸出:
10/4 -> 2.5
10/3 -> 3.33
10/2 -> 5
10/7 -> 1.42
3.9999 -> 3.99
更新*顯著*變化的問題。它不再要求*「舍入」*,而是*「截斷」*。當然,這仍然算作四捨五入(無論是趨向於零還是負無窮),但是當他們孤立地看到術語*「四捨五入」時,並不是人們通常所期望的。更正:這不是更新改變了問題。截斷要求從一開始就存在。建議的答案沒有解決這個問題。當在「3.1467665」上運行時,它會產生「3.15」。 – IInspectable