「javaround()」javaroundup函数
今天给各位分享javaround()的知识,其中也会对javaroundup函数进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
java Math.round() 随机数
我先问个问题,生成随机数的方法是round么? 是random吧?
这里有2个办法,一个是用Math,另一个是用Random类。
1、Math类
DecimalFormat df = new DecimalFormat("000");
int temp = (int) Math.random()*1000;
String randomNum = df.format(temp);
System.out.println(randomNum);
2、Random类
DecimalFormat df = new DecimalFormat("000");
Random r = new Random();
int temp = r.nextInt(1000);
String randomNum = df.format(temp);
System.out.println(randomNum);
java round函数怎么用
用于 小数Math.Round() ;函数 就是 去四舍五入 。
Math.Round(1.3) ; 结果为 1 ;Math.Round(1.6) ; 结果为 2 ;
多看看 Math 吧
Java中的Math.round()和Math.ceil(),Math.floor()区别
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
javaround()的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于javaroundup函数、javaround()的信息别忘了在本站进行查找喔。