divide With Math Rounding Mode And Scale - Java java.lang

Java examples for java.lang:Math Operation

Description

divide With Math Rounding Mode And Scale

Demo Code


//package com.java2s;
import java.math.BigDecimal;
import java.math.RoundingMode;

public class Main {
    private static BigDecimal b1;
    private static BigDecimal b2;

    public static String divideWithRoundingModeAndScale(String v1,
            String v2, RoundingMode roundingMode, int scale) {
        setDefalutValue(v1, v2);/*from   w  w w .j a  v a 2 s  . c o  m*/
        return b1.divide(b2, scale, roundingMode).toString();
    }

    private static void setDefalutValue(String v1, String v2) {
        if (v1 == null) {
            v1 = "0";
        }
        if (v2 == null) {
            v2 = "0";
        }
        b1 = new BigDecimal(v1);
        b2 = new BigDecimal(v2);
    }

    public static String divide(String v1, String v2) {
        setDefalutValue(v1, v2);
        return b1.divide(b2).toString();
    }
}

Related Tutorials