add Balance to Money - Java java.math

Java examples for java.math:BigDecimal Calculation

Description

add Balance to Money

Demo Code


import java.math.BigDecimal;
import java.security.MessageDigest;
import org.apache.log4j.Logger;

public class Main{
    public static void main(String[] argv) throws Exception{
        float balance = 2.45678f;
        float money = 2.45678f;
        int v = 2;
        System.out.println(addBalance(balance,money,v));
    }/*  w w w .  j  ava 2s  . c o m*/
    
    public static float addBalance(float balance, float money, int v) {
        if (balance != 0 && balance >= money) {
            return new BigDecimal(balance).add(new BigDecimal(money))
                    .setScale(v, BigDecimal.ROUND_HALF_UP).floatValue();
        }
        return 0;
    }
}

Related Tutorials