Thursday, December 5, 2019

To round-up till desire digits decimal format

/** @ To round-up digits two decimal format */


 /** @ To round-up digits two decimal format */
 private double RoundTo2Decimals(double val) {
  DecimalFormat df2 = new DecimalFormat("###.##");
  String as = df2.format(val);
  return Double.valueOf(as.replace(getDecimalSeparator(), "."));
 }



/** @ truncate decimal till desire  digits */

/** @ truncate decimal till 2 digits */
 protected double truncateDecimal(double x, int numberofDecimals) {
  BigDecimal num;
  if (x > 0) {
   num = new BigDecimal(String.valueOf(x)).setScale(numberofDecimals, BigDecimal.ROUND_FLOOR);
   return num.doubleValue();

  } else {
   num = new BigDecimal(String.valueOf(x)).setScale(numberofDecimals, BigDecimal.ROUND_CEILING);
   return num.doubleValue();
  }
 }
}



/** @ To round-up  till desire  digits decimal format with conditions */
protected  BigDecimal decimalDigiRoundUp(double x, int numberofDecimals) {
  BigDecimal num; String round1="###.#", round2="###.##";
  if (numberofDecimals ==2) {
   logger.info(new BigDecimal (new DecimalFormat(round2).format(x).replace(getDecimalSeparator(),".")));
          return new BigDecimal (new DecimalFormat(round2).format(x).replace(getDecimalSeparator(),"."));
  }else {
   logger.info(new DecimalFormat("###.#").format(x)+" Shree");
   return new BigDecimal (new DecimalFormat("###.#").format(x).replace(getDecimalSeparator(),".")+"0");
  }
 }



No comments:

Post a Comment