C#四舍五入(函数)用法实例
2018-10-12 14:06
483 查看
效果:
说明:输入小数,然后输入要保留的位数,
事件:点击Button
代码:
public static double Round(double d, int i)
{
if (d >= 0)
{
d += 5 * Math.Pow(10, -(i + 1));//求指定次数的指定次幂
}
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);//截取需要位数
}
if (poststr.Length <= 2)
{
poststr = poststr + "0";
}
string strd = prestr + "." + poststr;
d = double.Parse(strd);//将字符串转换为双精度实数
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())));
}
您可能感兴趣的文章:
相关文章推荐
- C#四舍五入用法实例
- C#中累加器函数Aggregate用法实例
- C#虚函数用法实例分析
- C# lock用法实例
- Python中函数的参数定义和可变参数用法实例分析
- 以代码为实例讲解sizeof函数易混淆的用法
- php中convert_uuencode()与convert_uuencode函数用法实例
- 【转】C#中dataGridView用法实例分析
- JavaScript中exec函数用法实例分析
- C#皮肤的用法(皮肤资源+使用实例下载)
- C#中GraphicsPath的AddString方法用法实例
- SeaJS中use函数用法实例分析
- 实例分享C#中Explicit和Implicit用法
- Python函数中的函数(闭包)用法实例
- php的dl函数用法实例
- python中偏函数partial用法实例分析
- C#中构造函数和析构函数用法实例详解
- jQuery中attr()与prop()函数用法实例详解(附用法区别)
- DOM节点深度克隆函数cloneNode()用法实例