Java examples for java.math:BigDecimal Calculation
add Balance to Money
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; } }