效果:
說明:輸入小數,然後輸入要保留的位數,
事件:點選button
**:複製** **如下:
public static double round(double d, int i)
else
&nwww.cppcns.combsp;
string str = d.tostring();
string strs = str.spl'.');
int idot = str.indexof('.');
string prestr = strs[0];
string poststr = strs[1];
if (poststr.length > i)
if (poststr.length <= 2)
string strd = prestr + "." + poststr;
&nb程式設計客棧sp; d = double.parse(strd);//將字串轉換為雙精度實數
return d;
}private void button1_click(object sender, eventargs e)
本文標題: c#四捨五入(函式)用法例項
本文位址: /ruanjian/csharp/102193.html
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...