各种四舍五入的模式

package com.sc;

import java.math.RoundingMode;
import java.text.NumberFormat;

public class DoubleTest {
	public static void main(String[] args) {
//		test1();
		test2();
	}
	
	public static void test1(){
		double num1 = 2.123456789;
		System.out.println(Math.log(num1));
		System.out.println(Math.E);
		System.out.println(Math.PI);
		System.out.println(Math.cbrt(num1));
		System.out.println(Math.ceil(num1));
		System.out.println(Math.hypot(1, 2));
		System.out.println(Math.log(num1));
		System.out.println(Math.log(num1));
		System.out.println(Math.log(num1));
		System.out.println(Math.log(num1));
		System.out.println(Math.log(num1));
	}
	
	public static void test2(){
		double value = 1.23456789;
		for(int i = 0; i < 6; i++){
			System.out.println(round(value, i));
			System.out.println(round2(value, i));
			System.out.println(round3(value, i));
			System.out.println(round4(value, i));
			System.out.println(round5(value, i));
			System.out.println(round6(value, i));
			System.out.println(round7(value, i));
//			System.out.println(round8(value, i));
			System.out.println(round9(value, i));
			System.out.println("***************************************************");
		}
	}
	
	public static double round(double value, int scale){
		NumberFormat numberFormat = NumberFormat.getInstance();
		numberFormat.setRoundingMode(RoundingMode.HALF_UP);
		numberFormat.setMaximumFractionDigits(scale);
		numberFormat.setMinimumFractionDigits(scale);
		return Double.valueOf(numberFormat.format(value));
	}
	
	public static double round2(double value, int scale){
		NumberFormat numberFormat = NumberFormat.getInstance();
		numberFormat.setRoundingMode(RoundingMode.CEILING);
		numberFormat.setMaximumFractionDigits(scale);
		numberFormat.setMinimumFractionDigits(scale);
		return Double.valueOf(numberFormat.format(value));
	}
	
	public static double round3(double value, int scale){
		NumberFormat numberFormat = NumberFormat.getInstance();
		numberFormat.setRoundingMode(RoundingMode.valueOf(scale));
		numberFormat.setMaximumFractionDigits(scale);
		numberFormat.setMinimumFractionDigits(scale);
		return Double.valueOf(numberFormat.format(value));
	}
	
	public static double round4(double value, int scale){
		NumberFormat numberFormat = NumberFormat.getInstance();
		numberFormat.setRoundingMode(RoundingMode.DOWN);
		numberFormat.setMaximumFractionDigits(scale);
		numberFormat.setMinimumFractionDigits(scale);
		return Double.valueOf(numberFormat.format(value));
	}
	
	public static double round5(double value, int scale){
		NumberFormat numberFormat = NumberFormat.getInstance();
		numberFormat.setRoundingMode(RoundingMode.FLOOR);
		numberFormat.setMaximumFractionDigits(scale);
		numberFormat.setMinimumFractionDigits(scale);
		return Double.valueOf(numberFormat.format(value));
	}
	
	public static double round6(double value, int scale){
		NumberFormat numberFormat = NumberFormat.getInstance();
		numberFormat.setRoundingMode(RoundingMode.HALF_DOWN);
		numberFormat.setMaximumFractionDigits(scale);
		numberFormat.setMinimumFractionDigits(scale);
		return Double.valueOf(numberFormat.format(value));
	}
	
	public static double round7(double value, int scale){
		NumberFormat numberFormat = NumberFormat.getInstance();
		numberFormat.setRoundingMode(RoundingMode.HALF_EVEN);
		numberFormat.setMaximumFractionDigits(scale);
		numberFormat.setMinimumFractionDigits(scale);
		return Double.valueOf(numberFormat.format(value));
	}
	
	
	public static double round8(double value, int scale){
		NumberFormat numberFormat = NumberFormat.getInstance();
		numberFormat.setRoundingMode(RoundingMode.UNNECESSARY);
		numberFormat.setMaximumFractionDigits(scale);
		numberFormat.setMinimumFractionDigits(scale);
		return Double.valueOf(numberFormat.format(value));
	}
	
	public static double round9(double value, int scale){
		NumberFormat numberFormat = NumberFormat.getInstance();
		numberFormat.setRoundingMode(RoundingMode.UP);
		numberFormat.setMaximumFractionDigits(scale);
		numberFormat.setMinimumFractionDigits(scale);
		return Double.valueOf(numberFormat.format(value));
	}
}

你可能感兴趣的:(四舍五入)