``` package com.kflh.boxApi.utils.util; import java.math.BigDecimal; /** * @program: BoxApi * @description: 計算浮點數 * @author: eterntiyz * @create: 2019... ...
package com.kflh.boxApi.utils.util;
import java.math.BigDecimal;
/**
* @program: BoxApi
* @description: 計算浮點數
* @author: eterntiyz
* @create: 2019-01-17 11:10
*/
public class DoubleCalendar {
/**
* @Description: 浮點加法
* @Param: []
* @return: java.lang.Double
* @Author: tonyzhang
* @Date: 2019-01-17 11:27
*/
public static Double add(String str1,String str2) {
BigDecimal bignum1 = new BigDecimal(str1);
BigDecimal bignum2 = new BigDecimal(str2);
BigDecimal bignum3 = bignum1.add(bignum2);
return bignum3.doubleValue();
}
/**
* @Description: 浮點減法
* @Param: []
* @return: java.lang.Double
* @Author: tonyzhang
* @Date: 2019-01-17 11:27
*/
public static Double subtract(String str1,String str2) {
BigDecimal bignum1 = new BigDecimal(str1);
BigDecimal bignum2 = new BigDecimal(str2);
BigDecimal bignum3 = bignum1.subtract(bignum2);
return bignum3.doubleValue();
}
/**
* @Description: 浮點乘法
* @Param: str1為分母,str2為分子
* @return: java.lang.Double
* @Author: tonyzhang
* @Date: 2019-01-17 11:26
*/
public static Double multiply(String str1,String str2) {
BigDecimal bignum1 = new BigDecimal(str1);
BigDecimal bignum2 = new BigDecimal(str2);
BigDecimal bignum3 = bignum1.multiply(bignum2);
return bignum3.doubleValue();
}
/**
* @Description: 浮點除法
* @Param: []
* @return: java.lang.Double
* @Author: tonyzhang
* @Date: 2019-01-17 11:26
*/
public static Double divide(String str1,String str2) {
BigDecimal bignum1 = new BigDecimal(str1);
BigDecimal bignum2 = new BigDecimal(str2);
//參數意義.bignum1為分母,bignum2為分子,scale保留的位數,BigDecimal.ROUND_DOWN表示不進位
BigDecimal bignum3 = bignum1.divide(bignum2,2,BigDecimal.ROUND_DOWN);
return bignum3.doubleValue();
}
public static void main(String[] args) {
System.out.println(divide("4600.0","0.6"));
}
}