round value by BigDecimal - Android java.math

Android examples for java.math:BigDecimal

Description

round value by BigDecimal

Demo Code

import java.math.BigDecimal;

public class Main {

  public static Object round(Object value, int places) {
    // if (places < 0) throw new IllegalArgumentException();
    if (places < 0 || !(value instanceof Double)) {
      return value;
    }//  w  w  w.  j a  v a 2  s  . c o m
    BigDecimal bd = new BigDecimal((Double) value);
    bd = bd.setScale(places, BigDecimal.ROUND_HALF_UP);
    return bd.doubleValue();
  }

}

Related Tutorials