java double类型保留位数、double转int

时间:2025-02-18 07:51:34

记录一下小知识

保留小数点后一位

DecimalFormat df = new DecimalFormat("#.0");
(0.999999);

同理,保留小数点后两位

DecimalFormat df = new DecimalFormat("#.00");
(0.999999);

同理保留小数点后零位

DecimalFormat df = new DecimalFormat("#");
(0.999999);


转int

int num = (new Double(0.999999)).intValue();

int num = (int)0.999999;