c# Math类的 基础使用

来源:互联网 发布:照片漫画化软件 编辑:程序博客网 时间:2024/05/18 03:56
===========常用的Math类方法============================

1、Math.random() 随机函数,获取一个介于0和1之间的一个数字,0<=x<1这样的一个小数,小数位数为14位

=============================

2、Math.round()
四舍五入,获取一个和表达式或者数字最接近的整数
如下:
x=5.45
x=Math.round(x)
response.write(x)//输出5

x=-5.45
x=Math.round(x)
response.write(x)//输出-5

x=-5.55
x=Math.round(x)
response.write(x)//输出-6

但是在这里发现一个要大家注意的问题,如果

x=5.50
x=Math.round(x)
response.write(x)//输出6

但是如果x=-5.50

x=-5.50
x=Math.round(x)
response.write(x)//输出-5    需要注意一下~!

====生成随机数示例=================

========下面我们使用他和上面我们已经研究过的函数,获取一个随机的四位数。===
x=Math.random()         // 0-1间,含有14个小数位的小数
x=x*10000              //小数位向后移动4位,这时,整数位拥有了4位
x=Math.round(x)       //四舍五入,去掉后面的小数位,保留4个整数位
respons.write(x)     //输出4个随机的整数位
输出一个随机的四位数


=============================

3、Math.max()计算两个数字或者表达式中的最大值,并返回这个值。

x=Math.max(5,4)
trace(x)  //输出为5
x=10
y=20
x=Math.max(x,y)
response.write(x) //输出为20

=============================

4、Math.min()返回两个数字或者表达式中最小的一个值

x=Math.min(5,4)
trace(x)  //输出为4
x=10
y=20
x=Math.min(x,y)
response.write(x) //输出为10

========================================

5、Math.ceil() 取得指定的数字或表达式的上限的整数值

x=5.45
x=Math.ceil(x)
response.write(x)   //输出6

x=-5.45
x=Math.ceil(x)
response.write(x)//输出-5

=======================================

6、Math.floor()这个函数和Math.ceil()恰好相反,获得一个数字或表达式的一个下限的整数值

x=5.45
x=Math.floor(x)
response.write(x)//输出5

x=-5.45
x=Math.floor(x)
response.write(x)//输出-6

========================================

7、Math.abs()  计算绝对值公式
x=5;
y=-5;
x=Math.abs(x)
response.write(x)
//输出 5

y=Math.abs(y)
response.write(y)
//输出 5

8.System.Math.Exp(m*System.Math.Log(n))

以上表达式表示:n的m次方。

原创粉丝点击