public static double r(double d, int i) { double dd = d * Math.pow(10, i);
dd = Math.round(dd);
dd = dd / Math.pow(10, i);
return dd; } d: 数字 i: 位数
改造了一下/** * 提供精确的小数位四舍五入处理。 * * @param v * 需要四舍五入的数字 * * @param scale * 小数点后保留几位 * * @return 四舍五入后的结果(字符串) * */ public static String round(double v, int scale) { if (scale < 0) { throw new IllegalArgumentException( "The scale must be a positive integer or zero"); } BigDecimal b = new BigDecimal(Double.toString(v)); BigDecimal one = new BigDecimal("1"); DecimalFormat df1 = new DecimalFormat("0.##########"); double t=b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); return df1.format(t).toString(); }
public static String round(double v, int scale) { if (scale < 0) { throw new IllegalArgumentException( "The scale must be a positive integer or zero"); } BigDecimal b = new BigDecimal(Double.toString(v)); BigDecimal one = new BigDecimal("1"); DecimalFormat df1 = new DecimalFormat("0.##########"); double t=b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); return df1.format(t).toString(); }
SafeSoft() 为什么复制我的代码?分不是这么得来的!
两位小数 DecimalFormat df = new DecimalFormat("#0.##"); theValue= Double.parseDouble(df.format(doubleValue));
/** *将Double型四舍五入,只保留小数点两位 */ public static double doubleToDouble(double num) { NumberFormat form = NumberFormat.getInstance(); if (form instanceof DecimalFormat) { ( (DecimalFormat) form).applyPattern("#0.0#"); ( (DecimalFormat) form).setMaximumFractionDigits(2); } String value = form.format(num); return Double.parseDouble(value); } public static String intToString(int num) { String result = "0"; try { result = Integer.toString(num); } catch (Exception e) { } return result; } public static String floatToString(float num) { String result = "0"; try { result = Float.toString(num); } catch (Exception e) { } return result; } public static String doubleToString(double num) { String result = "0"; try { result = Double.toString(num); } catch (Exception e) { } return result; } }
{
double dd = d * Math.pow(10, i);
dd = Math.round(dd);
dd = dd / Math.pow(10, i);
return dd;
}
d: 数字 i: 位数
* 提供精确的小数位四舍五入处理。
*
* @param v
* 需要四舍五入的数字
*
* @param scale
* 小数点后保留几位
*
* @return 四舍五入后的结果(字符串)
*
*/ public static String round(double v, int scale) {
if (scale < 0) {
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
DecimalFormat df1 = new DecimalFormat("0.##########");
double t=b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
return df1.format(t).toString();
}
if (scale < 0) {
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
DecimalFormat df1 = new DecimalFormat("0.##########");
double t=b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
return df1.format(t).toString();
}
DecimalFormat df = new DecimalFormat("#0.##");
theValue= Double.parseDouble(df.format(doubleValue));
*将Double型四舍五入,只保留小数点两位
*/
public static double doubleToDouble(double num) {
NumberFormat form = NumberFormat.getInstance();
if (form instanceof DecimalFormat) {
( (DecimalFormat) form).applyPattern("#0.0#");
( (DecimalFormat) form).setMaximumFractionDigits(2);
}
String value = form.format(num);
return Double.parseDouble(value);
} public static String intToString(int num) {
String result = "0";
try {
result = Integer.toString(num);
}
catch (Exception e) { }
return result;
} public static String floatToString(float num) {
String result = "0";
try {
result = Float.toString(num);
}
catch (Exception e) {
}
return result;
} public static String doubleToString(double num) {
String result = "0";
try {
result = Double.toString(num);
}
catch (Exception e) {
}
return result;
}
}