android中如何实现除法的保留小数点后2位,四舍五入!
来源:学生作业帮助网 编辑:作业帮 时间:2024/07/13 15:41:25
![android中如何实现除法的保留小数点后2位,四舍五入!](/uploads/image/z/5441393-65-3.jpg?t=android%E4%B8%AD%E5%A6%82%E4%BD%95%E5%AE%9E%E7%8E%B0%E9%99%A4%E6%B3%95%E7%9A%84%E4%BF%9D%E7%95%99%E5%B0%8F%E6%95%B0%E7%82%B9%E5%90%8E2%E4%BD%8D%2C%E5%9B%9B%E8%88%8D%E4%BA%94%E5%85%A5%21)
android中如何实现除法的保留小数点后2位,四舍五入!
android中如何实现除法的保留小数点后2位,四舍五入!
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();
}