本文主要是介绍C#用Math.Round和double.TryParse方法实现四舍五入,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
一、涉及到的知识点
1.double.TryParse()方法
2.Math.Round(Decimal, Int32) 方法
3.comboBox1没有选项
二、示例
1.源码
2.生成
一、涉及到的知识点
1.double.TryParse()方法
详见本文作者写的其他文章,
C#用double.TryParse(String, Double)方法将字符串类型数字转换为数值类型-CSDN博客 https://wenchm.blog.csdn.net/article/details/135593983
2.Math.Round(Decimal, Int32) 方法
将小数值舍入到指定数量的小数位,并将中点值舍入到最接近的偶数。
public static decimal Round (decimal d, int decimals);参数
d
Decimal
要舍入的小数。decimals
Int32
返回值中的小数位数。返回
Decimal
最接近 d 的 decimals 位小数的数字。例外
ArgumentOutOfRangeException
decimals 小于 0 或大于 28。OverflowException
结果超出了 Decimal 的范围。注解
参数的值 decimals 范围为 0 到 28。此方法使用 的默认舍入约定 MidpointRounding.ToEven。在舍入中点值时,舍入算法执行相等测试。
Math.Round输出示例:
//示例
Console.WriteLine(Math.Round(3.44m, 1));
Console.WriteLine(Math.Round(3.45m, 1));
Console.WriteLine(Math.Round(3.46m, 1));
Console.WriteLine();Console.WriteLine(Math.Round(4.34m, 1));
Console.WriteLine(Math.Round(4.35m, 1));
Console.WriteLine(Math.Round(4.36m, 1));// The example displays the following output:
// 3.4
// 3.4
// 3.5
//
// 4.3
// 4.4
// 4.4
由输出结果看,Math.Round的结果不是严格意义的四舍五入,比如Round(3.45,1)=3.4就没有按四舍五入操作,但Round(4.35,1)=4.4就按四舍五入操作了。这是Math.Round定义的规则。
如果想要严格地执行四舍五入,就需要对Round方法进行重载。
3.comboBox1没有选项
当没有选项时,comboBox1!.SelectedIndex == -1;
二、示例
1.源码
//用Math.Round和double.TryParse方法实现四舍五入
namespace _048
{public partial class Form1 : Form{private TextBox?textBox1;private TextBox? textBox2;private Label?label1;private Label? label2;private TextBox? textBox3;private ComboBox? comboBox1;private Button? button1;public Form1(){InitializeComponent();Load += Form1_Load;}private void Form1_Load(object?sender, EventArgs e){// // textBox1// textBox1 = new TextBox{Location = new Point(12, 12),Name = "textBox1",Size = new Size(100, 23),TabIndex = 0};// // textBox2// textBox2 = new TextBox{Location = new Point(137, 12),Name = "textBox2",Size = new Size(100, 23),TabIndex = 1};// // label1// label1 = new Label{AutoSize = true,Location = new Point(116, 18),Name = "label1",Size = new Size(17, 17),TabIndex = 2,Text = "+"};// // label2// label2 = new Label{AutoSize = true,Location = new Point(12, 82),Name = "label2",Size = new Size(68, 17),TabIndex = 3,Text = "计算结果:"};// // textBox3// textBox3 = new TextBox{Location = new Point(137, 76),Name = "textBox3",Size = new Size(100, 23),TabIndex = 4};// // comboBox1// comboBox1 = new ComboBox{FormattingEnabled = true,Location = new Point(12, 47),Name = "comboBox1",Size = new Size(100, 25),TabIndex = 5,};comboBox1.Items.AddRange(["保留1位小数","保留2位小数","保留3位小数","保留4位小数"]);// // button1// button1 = new Button{Location = new Point(162, 44),Name = "button1",Size = new Size(75, 23),TabIndex = 6,Text = "计算",UseVisualStyleBackColor = true};button1.Click += Button1_Click;// // Form1// AutoScaleDimensions = new SizeF(7F, 17F);AutoScaleMode = AutoScaleMode.Font;ClientSize = new Size(250, 107);Controls.Add(button1);Controls.Add(comboBox1);Controls.Add(textBox3);Controls.Add(label2);Controls.Add(label1);Controls.Add(textBox2);Controls.Add(textBox1);Name = "Form1";Text = "四舍五入"; }private void Button1_Click(object? sender, EventArgs e){if(textBox1!.Text !=""&& textBox1!.Text != ""){if(comboBox1!.SelectedIndex != -1){if (double.TryParse(textBox1!.Text, out double temp1) &&double.TryParse(textBox2!.Text, out double temp2)){textBox3!.Text = //得到四舍五入后的值Math.Round(temp1 + temp2, comboBox1!.SelectedIndex + 1).ToString();}else{MessageBox.Show("输入数值不正确!", "提示!");}}elseMessageBox.Show("请选择正确的小数点后位数", "提示!");}elseMessageBox.Show("重新输入正确的数字!", "提示!");}}
}
2.生成
这篇关于C#用Math.Round和double.TryParse方法实现四舍五入的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!