BigInteger percent - Java java.lang

Java examples for java.lang:int

Description

BigInteger percent

Demo Code


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

public class Main {
    public static void main(String[] argv) throws Exception {
        BigInteger value = new BigInteger("1234");
        BigInteger total = new BigInteger("1234");
        System.out.println(percent(value, total));
    }//  w  ww .j  a va  2 s .c o m

    public static BigDecimal percent(BigInteger value, BigInteger total) {

        if (total == null || total.intValue() < 1) {
            return new BigDecimal("0");
        }
        if (value == null || value.intValue() < 1) {
            value = new BigInteger("0");
        }

        BigDecimal porc = new BigDecimal("100");
        return new BigDecimal(value).multiply(porc).divide(
                new BigDecimal(total), 2, BigDecimal.ROUND_HALF_EVEN);
    }
}

Related Tutorials