subtract Balance Money - Java java.math

Java examples for java.math:BigDecimal Calculation

Description

subtract Balance 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(subtractBalance(balance,money,v));
    }//from  ww w  .j av a2 s .  co m
    
    public static float subtractBalance(float balance, float money, int v) {
        if (balance != 0 && balance >= money) {
            return new BigDecimal(balance).subtract(new BigDecimal(money))
                    .setScale(v, BigDecimal.ROUND_HALF_UP).floatValue();
        }
        return 0;
    }
}

Related Tutorials