Java BigDecimal Round round(BigDecimal num, int scale)

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

Description

round

License

Apache License

Declaration

public static BigDecimal round(BigDecimal num, int scale) 

Method Source Code


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

import java.math.BigDecimal;

public class Main {

    public static BigDecimal round(BigDecimal num, int scale) {
        return num.divide(new BigDecimal("1"), scale, BigDecimal.ROUND_HALF_UP);
    }//from w  w  w .j av a2  s.c  o m

    public static BigDecimal round(String num, int scale) {
        num = checkNum(num);
        BigDecimal bd1 = new BigDecimal(num);
        return bd1.divide(new BigDecimal("1"), scale, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal divide(String num1, String num2, int scale) {
        num1 = checkNum(num1);
        if ("".equals(num2)) {
            num2 = "1";
        }
        BigDecimal bd1 = new BigDecimal(num1);
        BigDecimal bd2 = new BigDecimal(num2);
        return bd1.divide(bd2, scale, BigDecimal.ROUND_HALF_UP);
    }

    private static String checkNum(String num) {
        if ("".equals(num)) {
            num = "0";
        }
        return num;
    }
}

Related

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