本实例介绍如何使用
Math类的
Abs方法实现数字的绝对值。此方法有多种重载形式,本实例所用的重载形式如下:
public static decimal Abs (decimal value)
此重载形式返回Decimal数字的绝对值。运行程序,效果如图5.18所示。主要代码如下:
private void button1_Click(object sender, EventArgs e) {
sbyte sb1 = -16, sb2 = 16; float fl1 = -12.0f, fl2 = 12.0f; string str = "";
str += "sbyte类型,数值sb1:-16,数值sb2: 16" + "\n";
str += "数值sb1绝对值:" + Convert.ToString(Math.Abs(sb1)) + "\n"; str += "数值sb2绝对值" + Convert.ToString(Math.Abs(sb1)) + "\n"; str += "float类型,数值fl1:-12.0f,数值fl2: 12.0f \n";
str += "数值fl1绝对值:" + Convert.ToString(Math.Abs(fl1)) + "\n"; str += "数值fl2绝对值:" + Convert.ToString(Math.Abs(fl2)) + "\n";
MessageBox.Show(str,"数值绝对值",MessageBoxButtons.OK,rmation); }
图5.18 数字绝对值
5.2 控件数据处理技巧