在Java中,在进行交易之前,为什么要将余额与BigDecimal.ONE进行比较?

huangapple go评论84阅读模式
英文:

Why should I compare the balance with BigDecimal.ONE before a transaction in Java?

问题

我正在查看有关从一个账户向另一个账户进行财务交易的教程,在将金额从一个账户转移到另一个账户之前,他们进行了这两个比较:

fromAccount.getCurrentBalance().compareTo(BigDecimal.ONE) == 1
        && fromAccount.getCurrentBalance().compareTo(amount) == 1

我理解他们进行第二次比较是为了将余额与金额进行比较,但我不理解为什么他们要将余额与BigDecimal.ONE进行比较。有人可以解释一下吗?

我不明白这个比较是用来做什么的:

fromAccount.getCurrentBalance().compareTo(BigDecimal.ONE)

这是代码:

if(fromAccount.getCurrentBalance().compareTo(BigDecimal.ONE) == 1
        && fromAccount.getCurrentBalance().compareTo(amount) == 1
){
    fromAccount.setCurrentBalance(fromAccount.getCurrentBalance().subtract(amount));
    accountRepository.save(fromAccount);
    toAccount.setCurrentBalance(toAccount.getCurrentBalance().add(amount));
    accountRepository.save(toAccount);
    Transaction transaction = transactionRepository.save(new Transaction(0L, fromAccountNumber, amount, new Timestamp(System.currentTimeMillis())));
    return transaction;
}
return null;
英文:

I'm looking at a tutorial about a financial transaction from an account to another and before transfering the amount from an account to another they do these 2 comparations:

fromAccount.getCurrentBalance().compareTo(BigDecimal.ONE) == 1
                && fromAccount.getCurrentBalance().compareTo(amount) == 1

I understand that they do the second comparation to compare the balance with the amount, but what I don't understand is why they compare the balance with BigDecimal.ONE. Can somebody explain?

I don't understand for what is this comparation:

fromAccount.getCurrentBalance().compareTo(BigDecimal.ONE)

This is the code:

if(fromAccount.getCurrentBalance().compareTo(BigDecimal.ONE) == 1
        && fromAccount.getCurrentBalance().compareTo(amount) == 1
){
    fromAccount.setCurrentBalance(fromAccount.getCurrentBalance().subtract(amount));
    accountRepository.save(fromAccount);
    toAccount.setCurrentBalance(toAccount.getCurrentBalance().add(amount));
    accountRepository.save(toAccount);
    Transaction transaction = transactionRepository.save(new Transaction(0L,fromAccountNumber,amount,new Timestamp(System.currentTimeMillis())));
    return transaction;
}
return null;

答案1

得分: 1

这只是一个在Java中用于比较大于的BigDecimal条件签名。如果您的CurrentBalance变量数据类型为BigDecimal,那么您不能使用普通的整数条件签名,如==><BigDecimalcompareTo()方法返回值以确定等于大于小于操作。

BigDecimal的CompareTo()方法返回:

0:如果此BigDecimal的值等于作为参数传递的BigDecimal对象的值。
1:如果此BigDecimal的值大于作为参数传递的BigDecimal对象的值。
-1:如果此BigDecimal的值小于作为参数传递的BigDecimal对象的值。

如果我们考虑这一行:

fromAccount.getCurrentBalance().compareTo(BigDecimal.ONE) == 1

这一行在整数数据类型中可以简单地表示为:

if(fromAccount.getCurrentBalance() > 1)

如果我将您的两个条件转换为普通的整数数据类型操作,那么它会变成这样:

if(fromAccount.getCurrentBalance() > 1 && fromAccount.getCurrentBalance() > amount)

访问此处了解BigDecimalcompareTo()操作。链接

英文:

This is just a BigDecimal greater than conditional signature in java. if your CurrentBalance variable datatype is BigDecimal then you can't use the normal integer conditional signature like == or > or <. BigDecimal compareTo() method returns values to determine the equal, greater than and less than operations.

Big Decimal CompareTo() method returns

0 : if value of this BigDecimal is equal to that of BigDecimal object passed as parameter.
1 : if value of this BigDecimal is greater than that of BigDecimal object passed as parameter.
-1 : if value of this BigDecimal is less than that of BigDecimal object passed as parameter.

If we consider this line

fromAccount.getCurrentBalance().compareTo(BigDecimal.ONE) == 1

this line can simply means in an integer datatype

if(fromAccount.getCurrentBalance() > 1)

If I convert both of your conditions in a normal integer datatype operation then it will looks like this.

if(fromAccount.getCurrentBalance() > 1 && fromAccount.getCurrentBalance() > amount)

visit here to understand BigDecimal compareTo() operation.<br/>
https://www.geeksforgeeks.org/bigdecimal-compareto-function-in-java/

huangapple
  • 本文由 发表于 2020年5月30日 22:33:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/62103909.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定