javascript中怎样输出Math.ceil或Math.floor或Math.round的值?
答案:2 悬赏:50 手机版
解决时间 2021-03-09 08:49
- 提问者网友:藍了天白赴美
- 2021-03-08 11:32
javascript中怎样输出Math.ceil或Math.floor或Math.round的值?
最佳答案
- 五星知识达人网友:三千妖杀
- 2021-03-08 12:34
输出??
alert(Math.ceil(2.4));//3
alert(Math.floor(2.4));//2
alert(Math.round(2.5));//3
alert(Math.ceil(2.4));//3
alert(Math.floor(2.4));//2
alert(Math.round(2.5));//3
全部回答
- 1楼网友:迟山
- 2021-03-08 13:11
ceil方法:
static double ceil(double a)
返回值为double类型,返回一个大于或等于参数a的最小整数。即它返回一个整数,这个整数是所有大于等于a的整数中最小的一个。
floor方法:
static double floor(double a)
返回值为double类型,返回一个小于或等于参数a的最大整数。即它返回一个整数,这个整数是所有小于等于a的整数中最大的一个。
round方法:
//该方法为重载方法
static long round(double a)
static int round(float a)
返回最接近参数a的整数,该方法等同于math.floor(a + 0.5)并将结果转换为long或int类型,即四舍五入取整。
public class mathtest {
public static void main(string[] args) {
double[] nums = {-0.6, -1.5, -1, 0.5, 1.2, 1.8};
for(double n : nums) {
test(n);
}
}
public static void test(double a) {
system.out.println("math.ceil(" + a + ")=" + math.ceil(a));
system.out.println("math.floor(" + a + ")=" + math.floor(a));
system.out.println("math.round(" + a + ")=" + math.round(a));
}
}
运行结果:
math.ceil(-0.6)=-0.0
math.floor(-0.6)=-1.0
math.round(-0.6)=-1
math.ceil(-1.5)=-1.0
math.floor(-1.5)=-2.0
math.round(-1.5)=-1
math.ceil(-1.0)=-1.0
math.floor(-1.0)=-1.0
math.round(-1.0)=-1
math.ceil(0.5)=1.0
math.floor(0.5)=0.0
math.round(0.5)=1
math.ceil(1.2)=2.0
math.floor(1.2)=1.0
math.round(1.2)=1
math.ceil(1.8)=2.0
math.floor(1.8)=1.0
math.round(1.8)=2
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯