Java BigDecimal Round round(BigDecimal initData, int scale)

Here you can find the source of round(BigDecimal initData, int scale)

Description

round

License

Apache License

Declaration

public static BigDecimal round(BigDecimal initData, int scale) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.math.BigDecimal;

public class Main {

    public static double round(double v, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }/*from   w w  w . ja v  a 2 s.c  o  m*/
        BigDecimal b = new BigDecimal(Double.toString(v));
        BigDecimal one = new BigDecimal("1");
        return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static BigDecimal round(BigDecimal initData, int scale) {

        if (initData == null) {
            return null;
        }

        return initData.setScale(scale, BigDecimal.ROUND_HALF_UP);
    }
}

Related

  1. round(BigDecimal decimal)
  2. round(BigDecimal decimal)
  3. round(BigDecimal decimal, int decimalDigits)
  4. round(BigDecimal decimal, int precision)
  5. round(BigDecimal dividend, int divisor)
  6. round(BigDecimal money, int scale)
  7. round(BigDecimal num, int scale)
  8. round(BigDecimal number)
  9. round(BigDecimal v, int scale, int roundingMode)