moneyphp / money

PHP implementation of Fowler's Money pattern.
http://moneyphp.org
MIT License
4.6k stars 440 forks source link

Absolute difference #594

Closed eekes closed 4 years ago

eekes commented 4 years ago

I was working on a project where I needed the absolute difference of 2 Money objects so I thought it would be useful adding a method for it 🙂

UlrichEckhardt commented 4 years ago

There's an absolute() function which I'd suggest you use for that. The code then looks like:

return $this->subtract($subtrahend)->absolute();

This has the added benefit that it builds on other, proven functions instead of treating a string ($this->amount) like an integer. That integer may be unable to accurately represent the value, which is why there are arbitrary-precision calculator implementations (see static $calculator member). Lastly, difference has an existing meaning. I'd rather call this absoluteDifference() to make that clear.

Even with the above adjusted, I'd still rather not add this. For one, it can be implemented based on the existing interface. Further, its use case is a corner case (I can't even imagine what you need that for).

eekes commented 4 years ago

@UlrichEckhardt The reason I need this is for comparing two big sets of data and searching for inconsistencies (and being able to display how much they differ).

But I didn't know about the absolute function, so this can be closed I think, thanks for the info 🙂