C 四捨五入(2)

2021-06-05 16:24:12 字數 384 閱讀 2420

**自---

今天很驚奇,發現了c++的格式化輸出可以四捨五入地保留小數

首先我們設定cout.setf(ios::fixed);

setf是setflag的縮寫,msdn解釋為sets the specificed flags。呵呵,估計是和c++的封裝有關吧。

然後我們設定cout.prevision(2);這個是保留兩位小數,四捨五入

我們可以用以下這個程式進行測試

#include

using namespace std;

int main()

{float a=3.1456;

cout.setf(ios::fixed);

cout.precision(2);

cout<

c 四捨五入

在處理一些資料時,我們希望能用 四捨五入 法實現,但是c 採用的是 四捨六入五成雙 的方法,如下面的例子,就是用 四捨六入五成雙 得到的結果 double d1 math.round 1.25,1 1.2double d2 math.round 1.24,1 1.2double d3 math.ro...

c 四捨五入

math.round跟conver.toint32一樣,都是使用四捨六入五成雙的規則,例如 math.round 3.2 返回3 math.round 3.6 返回4 math.round 3.5 返回4 math.round 2.5 返回2 要做到四捨五入,可加上引數midpointroundin...

C 四捨五入

在處理一些資料時,我們希望能用 四捨五入 法實現,但是c 採用的是 四捨六入五成雙 的方法,如下面的例子,就是用 四捨六入五成雙 得到的結果 double d1 math.round 1.25,1 1.2double d2 math.round 1.24,1 1.2double d3 math.ro...