(其它)Math类的常用方法

本文详细介绍了Java中Math类的使用,包括获取小数的地板、天花板和四舍五入值,以及生成随机字符串的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

package com.gc.system;


public class Demo {
	public static void main(String[] args) {
		getFloorCeilRound();
		getMinMax();
		getRandomValue();
	}

	private static void getFloorCeilRound() {
		double d = 12.51;
		
		//小于的最大整数
		double floor = Math.floor(d);
		System.out.println(floor);//12.0
		
		//大于的最小整数
		double ceil = Math.ceil(d);
		System.out.println(ceil);//13.0
		
		//四舍五入
		long round = Math.round(d);
		System.out.println(round);//13
	}

	private static void getRandomValue() {
		String rand = "";
		for(int i=0;i<6;i++) {
			//Math.floor() 取小于x的最大整数,得到0-9的范围  //Math.ceil()  1-10
			double d = Math.floor(Math.random()*10);// 0-9.9999
			rand += (int)d;
		}
		System.out.print(rand);
	}

	private static void getMinMax() {
		int a = 10;
		int b = 5;
		
		int min = Math.min(a, b);
		System.out.println(min);
		
		int max = Math.max(a, b);
		System.out.println(max);
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值