C#四舍五入(函數(shù))用法實例
效果:

說明:輸入小數(shù),然后輸入要保留的位數(shù),
事件:點擊Button
代碼:
public static double Round(double d, int i)
{
if (d >= 0)
{
d += 5 * Math.Pow(10, -(i + 1));//求指定次數(shù)的指定次冪
}
else
{
d += 5 * Math.Pow(10, -(i + 1));
}
string str = d.ToString();
string[] strs = str.Split('.');
int idot = str.IndexOf('.');
string prestr = strs[0];
string poststr = strs[1];
if (poststr.Length > i)
{
poststr = str.Substring(idot + 1, i);//截取需要位數(shù)
}
if (poststr.Length <= 2)
{
poststr = poststr + "0";
}
string strd = prestr + "." + poststr;
d = double.Parse(strd);//將字符串轉(zhuǎn)換為雙精度實數(shù)
return d;
}
private void button1_Click(object sender, EventArgs e)
{
textBox3.Text=Convert.ToString(Math.Round(Convert.ToDouble(textBox1.Text.Trim()),Convert.ToInt16(textBox2.Text.Trim())));
}
相關(guān)文章
C# 使用 WebBrowser 實現(xiàn) HTML 轉(zhuǎn)圖片功能的示例代碼
這篇文章主要介紹了C# 如何使用 WebBrowser 實現(xiàn) HTML 轉(zhuǎn)圖片功能,文中示例代碼非常詳細,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
C#中數(shù)據(jù)的傳遞以及ToolStripProgressBar
本文主要介紹了C#的數(shù)據(jù)傳遞方法以及ToolStripProgressBar進度條的使用。希望對大家有所幫助,話不多說,請看下面代碼2016-11-11
WPF 在image控件用鼠標(biāo)拖拽出矩形的實現(xiàn)方法
這篇文章主要介紹了WPF 在image控件用鼠標(biāo)拖拽出矩形的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08

