BigDecimal计算
import java.math.BigDecimal;
/**
* BigDecimal 类操作
* @author Administrator
*
*/
public class BigDecimalTools {
/**
* 小数计算
*/
public void computeNum(){
BigDecimal shopPrice_b = new BigDecimal(150);
BigDecimal shopNum_b = new BigDecimal(6);
BigDecimal shopNumPrice = shopNum_b.multiply(shopPrice_b); //总额
BigDecimal golden_gzj = shopNumPrice.multiply(new BigDecimal("0.05"));
BigDecimal manage_cost = shopNumPrice.multiply(new BigDecimal("0.05"));
BigDecimal balance_ye = shopNumPrice.multiply(new BigDecimal("0.9"));
System.out.println(golden_gzj);
System.out.println(manage_cost);
System.out.println(balance_ye);
}
/**
* 整数计算
*/
public void computeNum2(){
//获取整数
BigDecimal bd = new BigDecimal("12.7");
long s1 = bd.setScale( 0, BigDecimal.ROUND_UP ).longValue(); // 向上取整
long s2 = bd.setScale( 0, BigDecimal.ROUND_DOWN ).longValue(); // 向下取整
System.out.println("ww======="+s1);
System.out.println("ww======="+s2);
}
/**
* 乘法、除法计算
* @param args
*/
public static void computeNum3(){
BigDecimal bignum1 =new BigDecimal(15.2);
BigDecimal bignum2 =new BigDecimal(1.19);
//乘法保留整数位(四舍五入)
BigDecimal sellPrice = bignum1.multiply(bignum2).setScale(2,BigDecimal.ROUND_HALF_UP);
BigDecimal balanceNum = new BigDecimal(200); //余额
//除法,保留整数位(去掉小数位)
BigDecimal buyMaxNum = balanceNum.divide(bignum1,0,BigDecimal.ROUND_DOWN);
System.out.println("1-----"+sellPrice);
System.out.println("2-----"+buyMaxNum);
}
public static void main(String[] args) {
computeNum3();
}
}