13、Math类简介

Math类概述

在java.lang包下,有个Math类,这个类包含用于执行基本数学运算的方法,如四舍五入,开方等等。

package com.sutaoyu.usually_class;

public class String_test10 {
    public static void main(String[] args) {
        //圆周率
        System.out.println(Math.PI); //3.141592653589793
        
        //取绝对值
        System.out.println(Math.abs(-10)); //10
        
        //ceil天花板,会向上取值,结果是double
        System.out.println(Math.ceil(12.11));//13.0
        System.out.println(Math.ceil(9.89));//10.0
        
        //floor地板,会向下取整,结果是double
        System.out.println(Math.floor(11.11));//11.0
        System.out.println(Math.floor(7.89));//7.0
        
        //获取两个值中的最大值
        System.out.println(Math.max(100, 80));//100
        
        //前面的数是底数,后面的数是指数,即2的3次方
        System.out.println(Math.pow(2, 3));//8.00
        
        //生成0.0到1.0之间的随机小数,包括0.0,不包括1.0
        System.out.println(Math.random()); //0.7952284902360376
        
        //四舍五入
        System.out.println(Math.round(12.3f));//12
        System.out.println(Math.round(9.8f));//10
        
        //开平方
        System.out.println(Math.sqrt(27));//5.196152422706632
        
    }
}

 

转载于:https://www.cnblogs.com/zhuifeng-mayi/p/10123492.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值