作业帮 > 英语 > 作业

android中如何实现除法的保留小数点后2位,四舍五入!

来源:学生作业帮 编辑:拍题作业网作业帮 分类:英语作业 时间:2024/05/16 02:33:19
android中如何实现除法的保留小数点后2位,四舍五入!
static long round(double a) Returns the closest long to the argument.
static int round(float a) Returns the closest int to the argument
精确的,是这样.
/**
* 提供精确的小数位四舍五入处理.
*
* @param v
* 需要四舍五入的数字
* @param scale
* 小数点后保留几位
* @return 四舍五入后的结果
*/
public static double round(Double v, int scale) {
if (scale < 0) {
throw new IllegalArgumentException("The scale must be a positive integer or zero");
}
BigDecimal b = null == v ? new BigDecimal("0.0") : new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
再问: 能不能给一个最简单的Java例子!麻烦你了!
再答: 拜托了,android的例子,在android安装目录有不少,有简单、有容易的,用上这段就可以了。