AVA取整及び四捨五入

3191 ワード

AVA取整及び四捨五入
import java.math.BigDecimal;



public class Test {

    public static void main(String[] args) {

        double i = 3.856;

        //  

        System.out.println(" :Math.floor(3.856)=" + (int) Math.floor(i));

        //  

        System.out.println(" :(3.856)="

                + new BigDecimal(i).setScale(0, BigDecimal.ROUND_HALF_UP));

        //  

        System.out.println(" :(3.856)="

                + new BigDecimal(i).setScale(2, BigDecimal.ROUND_HALF_UP));

        //

        System.out.println(" :Math.ceil(3.856)=" + (int) Math.ceil(i));

        //  

        System.out.println(" :Math.floor(-3.856)=" + (int) Math.floor(-i));

        //  

        System.out.println(" :(-3.856)="

                + new BigDecimal(-i).setScale(0, BigDecimal.ROUND_HALF_UP));

        //  

        System.out.println(" :(-3.856)="

                + new BigDecimal(-i).setScale(2, BigDecimal.ROUND_HALF_UP));

        //

        System.out.println(" (-3.856)=" + (int) Math.ceil(-i));

//         

        System.out.println((int) Math.ceil((float) 21 / (float) 30));

    }

}

実行結果:
小数を切り捨てる:Math.floor(3.856)=3四捨五入整:(3.856)=4四捨五入整:(3.856)=3.86湊整:Math.Ceil(3.856)=4小数点を切り捨てる:Math.floor(-3.856)=-4四捨五入:(-3.856)=-4四捨五入:(-3.856)=-3.86湊整(-3.856)=-31