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

Unit math operations on integer Decimal values return integer rather than Decimal #49

Open
glorieux opened this issue Nov 21, 2024 · 0 comments

Comments

@glorieux
Copy link

Hello,

There seems to be an issue when using the math functions with Decimal values.

Here is a quick example to illustrate the problem:

> Cldr.Unit.add(Cldr.Unit.new!(:unit, Decimal.new(42)), Cldr.Unit.new!(:unit, Decimal.new(2))) 
|> Cldr.Unit.value()
44

In that case I would expect the value to be Decimal.new("44").

It seems to work fine if I introduce a float:

> Cldr.Unit.add(Cldr.Unit.new!(:unit, Decimal.new(42)), Cldr.Unit.new!(:unit, Decimal.from_float(2.2))) 
|> Cldr.Unit.value()
Decimal.new("44.2")

I've narrowed the origin of the problem to that call to Cldr.Math.maybe_integer() on every math operations.

I would expect the returns value to be a Decimal if I follow the logic from cldr_utils math functions documentation:

 Adds two numbers together.

 The numbers can be integers, floats or Decimals.
 The type of the return will be Decimal if the
  either of the arguments is a Decimal.

 If both arguments are integers, the result will
 be an integer. If either of the arguments is a float,
 the result will be a float.

Is there any reason those calls to Cldr.Math.maybe_integer() are required?
If not how do you want to proceed to remove them (I could make a pull request)?

In any case thanks for that library it already saved me so much time and headaches 🙏.

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

1 participant