/**
* double 四舍五入
*/
public class CalculationUtils {
/**
* double 类型 四舍五入保留两位小数, 返回百分比 int类型
* @param d
* @return
*/
public static int doubleToIntPercentage(Double d){
BigDecimal b = new BigDecimal(d);
double f1 = b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
int result = (int) (f1 * 100);
return result;
}
public static void main(String[] args) {
double d = 0.2897;
BigDecimal b = new BigDecimal(d);
double f1 = b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
int c =(int) (f1 * 100f);
System.out.println(f1);
System.out.println(c);
}
}
double 四舍五入
于 2024-01-26 11:38:29 首次发布