如何使用PHP库brick/money比较金额

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

How to compare amounts with PHP library brick/money

问题

I decided to use the PHP library brick/money for currency calculations, since it's too imprecise to use floats, and it's annoying to convert amounts to cents so you use integers only, and to make easier dealing with rounding. I didn't go with the money PHP library because it's not flexible and demands inputs to be in integer-ish form (and yeah, I'm lazy to make input/output wrappers and don't want to be debugging in cents).

我决定使用PHP库brick/money进行货币计算,因为使用浮点数太不精确,而且将金额转换为分以便仅使用整数并更容易处理舍入很烦人。我没有选择money PHP库,因为它不灵活,要求输入必须以整数形式提供(是的,我懒得创建输入/输出包装器,也不想在分方面进行调试)。

I'm noticing a tiny downside of using brick/money since there isn't good documentation. I searched for a direct way to compare two amounts and this is a question I couldn't find an official/straightforward/direct form to do it.

我注意到使用brick/money有一个小小的缺点,因为没有很好的文档。我搜索了一种直接比较两个金额的方法,但我没有找到官方/直接的方式来执行此操作。

So I have something like:

use Brick\Money\Money;

$money1 = Money::of(50, 'MXN'); // MXN 50.00
$money1 = $money1->plus('10'); // MXN 60.00 -- whatever operation

$money2 = Money::of(50, 'MXN'); // MXN 50.00
$money2 = $money1->minus('10'); // MXN 40.00 -- whatever operation

因此,我有类似以下的代码:

use Brick\Money\Money;

$money1 = Money::of(50, 'MXN'); // MXN 50.00
$money1 = $money1->plus('10'); // MXN 60.00 -- 无论是什么操作

$money2 = Money::of(50, 'MXN'); // MXN 50.00
$money2 = $money1->minus('10'); // MXN 40.00 -- 无论是什么操作

Question:
How can I compare the two amounts in an easier way? Do I need to call ->getMinorAmount()->toInt() for each comparison?
Do I need to do something like the following for basic validation?:

问题:
如何以更简单的方式比较这两个金额?我需要为每个比较调用->getMinorAmount()->toInt()吗?
我是否需要执行以下类似的操作进行基本验证?:

if ($money1->getMinorAmount()->toInt() < 0 || 
 $money1->getMinorAmount()->toInt() < $money2->getMinorAmount()->toInt()) {
   $error = true;
}

Meanwhile in money PHP you have functions like this:

$result = $value1->equals($value2); 
$result = $value1->greaterThan($value2);
$result = $value1->greaterThanOrEqual($value2);
//etc...

与此同时,在money PHP中,您可以使用类似这样的函数:

$result = $value1->equals($value2); 
$result = $value1->greaterThan($value2);
$result = $value1->greaterThanOrEqual($value2);
//等等...

Is this a missing feature or am I missing something? Thanks in advance.

这是一个缺失的功能吗,还是我漏掉了什么?提前感谢。

英文:

I decided to use the PHP library brick/money for currency calculations, since it's too imprecise to use floats, and it's annoying to convert amounts to cents so you use integers only, and to make easier dealing with rounding. I didn't go with the money PHP library because it's not flexible and demands inputs to be in integer-ish form (and yeah, I'm lazy to make input/output wrappers and don't want to be debugging in cents).

I'm noticing a tiny downside of using brick/money since there isn't good documentation. I searched for a direct way to compare two amounts and this is a question I couldn't find an official/straightforward/direct form to do it.

So I have something like:

use Brick\Money\Money;

$money1 = Money::of(50, &#39;MXN&#39;); // MXN 50.00
$money1 = $money1-&gt;plus(&#39;10&#39;); // MXN 60.00 -- whatever operation

$money2 = Money::of(50, &#39;MXN&#39;); // MXN 50.00
$money2 = $money1-&gt;minus(&#39;10&#39;); // MXN 40.00 -- whatever operation

Question:
How can I compare the two amounts in an easier way? Do I need to call -&gt;getMinorAmount()-&gt;toInt() for each comparison?
Do I need to do something like the following for basic validation?:

if ($money1-&gt;getMinorAmount()-&gt;toInt() &lt; 0 || 
 $money1-&gt;getMinorAmount()-&gt;toInt() &lt; $money2-&gt;getMinorAmount()-&gt;toInt()) {
   $error = true;
}

Meanwhile in money PHP you have functions like this:

$result = $value1-&gt;equals($value2); 
$result = $value1-&gt;greaterThan($value2);
$result = $value1-&gt;greaterThanOrEqual($value2);
//etc...

Is this a missing feature or am I missing something? Thanks in advance.

答案1

得分: 1

AbstractMoney类中有类似的方法:

$result = $money1->isEqualTo($money2); 
$result = $money1->isGreaterThan($money2);
$result = $money1->isGreaterThanOrEqualTo($money2);
...
英文:

There're similar methods in the AbstractMoney class

$result = $money1-&gt;isEqualTo($money2); 
$result = $money1-&gt;isGreaterThan($money2);
$result = $money1-&gt;isGreaterThanOrEqualTo($money2);
...

huangapple
  • 本文由 发表于 2023年4月13日 14:52:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002459.html
匿名

发表评论

匿名网友

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

确定