Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the right way to compare 2 currency values? #303

Open
pkozuchowski opened this issue Mar 31, 2021 · 1 comment
Open

What is the right way to compare 2 currency values? #303

pkozuchowski opened this issue Mar 31, 2021 · 1 comment

Comments

@pkozuchowski
Copy link

pkozuchowski commented Mar 31, 2021

I don't see any method like equals to compare currency1.equals(currency2). What is the right way for comparing values?
Is c1.value === c2.value safe considering floating point precision?

Please take under consideration implementing .equals, or .compare() method :)

@scurker
Copy link
Owner

scurker commented Mar 31, 2021

Right now, there's no built in way to determine equality, but both .value and .intValue are safe for comparisons because the values are normalized.

var c1 = currency(2.52).subtract(.01);
var c2 = currency(2.50).add(.01);
c1.value === c2.value; // => true

There is a babel plugin that makes it so you can do straight up comparisons:

currency(1.23) === currency(1.23); // => true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants