源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C#四舍五入(函数)用法实例

  • 时间:2022-04-27 17:02 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C#四舍五入(函数)用法实例
效果: [img]http://files.jb51.net/file_images/article/201311/20131128165627726.png[/img] 说明:输入小数,然后输入要保留的位数, 事件:点击Button 代码:
[u]复制代码[/u] 代码如下:
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())));         }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部