Java中四舍五入

大兔子大兔子 提交于 2019-11-27 06:16:24

1、Math中四舍五入的方法

     Math.ceil(double a)向上舍入,将数值向上舍入为最为接近的整数,返回值是double类型

     Math.floor(double a)向下舍入,将数值向下舍入为最为接近的整数,返回值是double类型

     Math.round(float a)标准舍入,将数值四舍五入为最为接近的整数,返回值是int类型

     Math.round(double a)标准舍入,将数值四舍五入为最为接近的整数,返回值是long类型

2、Math中random生成随机数

     Math.random()生成大于等于0,小于1的随机数

3、Random类生成随机数

     两种构造方式:第一种使用默认的种子(当前时间作为种子),另一个使用long型整数为种子,Random类可以生成布尔型、浮点类型、整数等类型的随机数,还可以指定生成随机数的范围

4、BigDecimal处理小数

    两种构造方式:第一种直接value写数字的值,第二种用String

import java.math.BigDecimal;import java.util.Random;import java.util.concurrent.ThreadLocalRandom;public class TestNumber {    public static void main(String[] args){        //ceil返回大的值        System.out.println(Math.ceil(-10.1));   //-10.0        System.out.println(Math.ceil(10.7));    //11.0        System.out.println(Math.ceil(-0.7));    //-0.0        System.out.println(Math.ceil(0.0));     //0.0        System.out.println(Math.ceil(-0.0));    //-0.0        System.out.println(Math.ceil(-1.7));    //-1.0        //floor返回小的值        System.out.println(Math.floor(-10.1));   //-11.0        System.out.println(Math.floor(10.7));    //10.0        System.out.println(Math.floor(-0.7));    //-1.0        System.out.println(Math.floor(0.0));     //0.0        System.out.println(Math.floor(-0.0));    //-0.0        System.out.println(Math.floor(-1.7));    //-2.0        //round四舍五入,float返回int,double返回long        System.out.println(Math.round(10.5));   //11        System.out.println(Math.round(-10.5));  //-10        //Math生成随机数        System.out.println(Math.random());        //Random类生成随机数        Random rand=new Random();        System.out.println(rand.nextBoolean());        System.out.println(rand.nextDouble());        System.out.println(rand.nextInt());        System.out.println(rand.nextInt(10));        //Random使用当前时间作为Random的种子        Random rand2 = new Random(System.currentTimeMillis());        System.out.println(rand2.nextBoolean());        System.out.println(rand2.nextDouble());        System.out.println(rand2.nextInt());        System.out.println(rand2.nextInt(10));        System.out.println(rand2.nextInt(5));        //ThreadLocalRandom        ThreadLocalRandom rand3 = ThreadLocalRandom.current();        System.out.println(rand3.nextInt(5,10));        //BigDecimal        System.out.println(0.8 - 0.7);   //0.10000000000000009        BigDecimal a1=new BigDecimal(0.1);        BigDecimal b1=new BigDecimal(0.9);        BigDecimal c1=a1.add(b1);        System.out.println("a1.add(b1)="+c1);  //a1.add(b1)=1.0000000000000000277555756156289135105907917022705078125        BigDecimal a2=new BigDecimal("0.1");        BigDecimal b2=new BigDecimal("0.9");        BigDecimal c2=a2.add(b2);        System.out.println("a2="+a2);  //a2=0.1        System.out.println("a2.add(b2)="+c2);  //a2.add(b2)=1.0    } }

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!