BigDecimal divide - Java java.math

Java examples for java.math:BigDecimal Calculation

Description

BigDecimal divide

Demo Code

/*/*from   ww  w  .ja va 2  s .c o m*/
 * Copyright (c) 2005-2009, Topicus b.v.
 * All rights reserved
 */
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.List;

public class Main{
    /**
     * @param decimal
     * @param by
     * @return decimal / by
     */
    public static final BigDecimal divide(BigDecimal decimal, int by) {
        return decimal.divide(BigDecimal.valueOf(by));
    }
    /**
     * @param decimal
     * @param mc
     * @param by
     * @return decimal / by afgerond volgens de context
     */
    public static final BigDecimal divide(BigDecimal decimal,
            MathContext mc, int by) {
        return decimal.divide(BigDecimal.valueOf(by), mc);
    }
    /**
     * @param decimal
     * @param mc
     * @param by
     * @return decimal / by afgerond volgens de context
     */
    public static final BigDecimal divide(BigDecimal decimal,
            MathContext mc, BigDecimal by) {
        return decimal.divide(by, mc);
    }
    /**
     * @param decimal
     * @param by
     * @return decimal / by
     */
    public static final BigDecimal divide(int decimal, int by) {
        return BigDecimal.valueOf(decimal).divide(BigDecimal.valueOf(by));
    }
    /**
     * @param decimal
     * @param mc
     * @param by
     * @return decimal / by afgerond volgens de context
     */
    public static final BigDecimal divide(int decimal, MathContext mc,
            int by) {
        return BigDecimal.valueOf(decimal).divide(BigDecimal.valueOf(by),
                mc);
    }
    /**
     * Maakt een BigDecimal van de gegeven string.
     * 
     * @param string
     * @return BigDecimal representatie van de gegeven string.
     */
    public static final BigDecimal valueOf(String string) {
        return new BigDecimal(localizeString(string));
    }
    private static final String localizeString(String org) {
        int punten = StringUtil.countOccurances(org, '.');
        int comma = StringUtil.countOccurances(org, ',');
        if (punten == 1 && comma == 0)
            return org;
        else if (comma == 1 && punten == 0)
            return org.replace(',', '.');
        else if (punten > 1 && comma > 1)
            throw new IllegalArgumentException(
                    "Ongeldig aantal punten en comma's");

        int puntIndex = punten > 0 ? org.indexOf('.') : -1;
        int commaIndex = comma > 0 ? org.indexOf(',') : -1;
        if (before(puntIndex, commaIndex)) {
            if (comma > punten)
                throw new IllegalArgumentException(
                        "Er zijn meer comma's dan punten maar de punt komt eerst");
            String[] tokens = org.split("\\.", -1);
            return handleParts(org, tokens, ',', true);
        } else if (before(commaIndex, puntIndex)) {
            if (punten > comma)
                throw new IllegalArgumentException(
                        "Er zijn meer punten dan comma's maar de comma komt eerst");
            String[] tokens = org.split(",", -1);
            return handleParts(org, tokens, '.', false);
        } else
            // geen punten of comma's
            return org;
    }
    private static final boolean before(int index1, int index2) {
        return (index2 == -1 && index1 >= 0)
                || (index1 >= 0 && index1 < index2);
    }
    /**
     * @param org
     * @param tokens
     * @return
     */
    private static final String handleParts(String org, String[] tokens,
            char decimalSeparator, boolean replaceDot) {
        String result = validateString(tokens, decimalSeparator);
        if (result != null)
            throw new IllegalArgumentException("\"" + result
                    + "\" is geen geldig duizendtal.");
        if (replaceDot)
            tokens[tokens.length - 1] = tokens[tokens.length - 1].replace(
                    ',', '.');
        StringBuilder builder = new StringBuilder(org.length());
        for (int i = 0; i < tokens.length; i++) {
            builder.append(tokens[i]);
        }
        return builder.toString();
    }
}

Related Tutorials