divide With Rounding Mode - Java java.lang

Java examples for java.lang:Math Operation

Description

divide With Rounding Mode

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 divideWithRoundingMode(String v1, String v2,
            RoundingMode roundingMode) {
        return divideWithRoundingModeAndScale(v1, v2, RoundingMode.DOWN, 0);
    }/*from  www.j a v a2 s  .  co m*/

    public static String divideWithRoundingModeAndScale(String v1,
            String v2, RoundingMode roundingMode, int scale) {
        setDefalutValue(v1, v2);
        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