package com.btzh.mis.house.utils;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
/**
* Double類型數據處理類
* @author weijixiang
* @date 2017/10/17.
*/
public class NumberUtil {
public static Double saveOneBit(Double d){
DecimalFormat format = new DecimalFormat("#0.###");
format.setRoundingMode(RoundingMode.FLOOR);
String result = format.format(d);
return Double.parseDouble(result);
}
/**
* 保留一位小數,不進行四捨五入
* @param d
* @return
*/
public static Double saveOneBitOne(Double d){
BigDecimal bd = new BigDecimal(d);
Double tem = bd.setScale(1,BigDecimal.ROUND_FLOOR).doubleValue();
return tem;
}
/**
* 保留一位小數,進行四捨五入
* @param d
* @return
*/
public static Double saveOneBitOneRound(Double d){
BigDecimal bd = new BigDecimal(d);
Double tem = bd.setScale(1,BigDecimal.ROUND_HALF_UP).doubleValue();
return tem;
}
/**
* 保留兩位小數,不進行四捨五入
* @param d
* @return
*/
public static Double saveOneBitTwo(Double d){
BigDecimal bd = new BigDecimal(d);
Double tem = bd.setScale(2,BigDecimal.ROUND_FLOOR).doubleValue();
return tem;
}
/**
* 保留兩位小數,進行四捨五入
* @param d
* @return
*/
public static Double saveOneBitTwoRound(Double d){
BigDecimal bd = new BigDecimal(d);
Double tem = bd.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
return tem;
}
/**
* 保留一位小數,進行四捨五入(該方法經測試 較爲精準)
* @param d
* @return
*/
public static Double saveOneBitOneRound(Double d){
String str = String.format("%.1f",d);
double c = Double.parseDouble(str);
return c;
}
public static void main(String [] args){ double a = saveOneBitOneRound(1200.48); System.out.println(a); double b = Double.parseDouble(String.valueOf(a)); System.out.println(b); }}