C#四舍五入(函数)用法实例
时间:2022-02-03 21:51:16
效果:
说明:输入小数,然后输入要保留的位数,
事件:点击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#,四舍五入
0
投稿
猜你喜欢
mybatis批量新增、删除、查询和修改方式
2023-11-23 10:13:01
Android获取手机的版本号等信息的代码
2021-09-21 06:00:26
C#读取二进制文件方法分析
2022-04-23 11:17:43
Android Studio实现简单计算器功能
2023-10-17 03:54:42
mybatis mybatis-plus-generator+clickhouse自动生成代码案例详解
2021-06-06 10:12:55
Spring的事务机制实例代码
2021-09-11 07:46:23
C#中的一些延时函数
2023-11-29 04:33:30
Android使用多线程进行网络聊天室通信
2022-05-11 18:56:36
Java实现文件上传到服务器本地并通过url访问的方法步骤
2021-12-01 11:45:20
详解Java中Collections.sort排序
2023-08-20 00:51:48
详解Android中PopupWindow在7.0后适配的解决
2022-12-07 11:16:59
Java硬币翻转倍数递增试算实例
2021-09-29 08:39:22
SpringBoot中多环境配置和@Profile注解示例详解
2023-11-29 05:39:04
c# 引用类型和值类型
2023-10-11 08:20:48
Java数据结构之环形链表和约瑟夫问题详解
2023-07-19 11:02:32
springboot如何静态加载@configurationProperties
2021-12-13 16:20:13
java用接口、多态、继承、类计算三角形和矩形周长及面积的方法
2021-10-24 22:15:46
如何把spring boot项目部署到tomcat容器中
2023-10-08 18:53:51
MyBatis字段名和属性名不一致的解决方法
2022-12-15 18:15:22
详解java并发之重入锁-ReentrantLock
2021-08-03 03:58:08