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

Cldr.Unit.Conversion does not deal with decimal values #8

Closed
LostKobrakai opened this issue Aug 15, 2018 · 5 comments
Closed

Cldr.Unit.Conversion does not deal with decimal values #8

LostKobrakai opened this issue Aug 15, 2018 · 5 comments

Comments

@LostKobrakai
Copy link
Contributor

LostKobrakai commented Aug 15, 2018

As cldr is already requiring the decimal library, couldn't Cldr.Unit normalize to just using Decimal internally?

test "decimal" do
    unit = Cldr.Unit.new(Decimal.new("300"), :minute)

    hours = Cldr.Unit.Conversion.convert(unit, :hour)

    assert hours.unit == :hour
    assert Decimal.equal?(5, Cldr.Unit.to_value(hours))
end
@kipcole9
Copy link
Collaborator

Yes, it should support Decimal since all the Cldr libs should be consistent in this respect. Will add it to the weekend list to fix.

@kipcole9
Copy link
Collaborator

Fixed, sorry it took so long. Published on hex as version 1.2.2

@LostKobrakai
Copy link
Contributor Author

Can we make cldr_unit use only decimals internally? ex_cldr does depend on decimal anyways and results like the ones below just aren't nice to work with. (this is on 1.3.0)

iex(12)> Cldr.Unit.new!(Decimal.new(28800), :second) |> Cldr.Unit.convert(:minute)
#Unit<:minute, #Decimal<480.009600>>

@kipcole9
Copy link
Collaborator

kipcole9 commented Jan 8, 2019

Actually its all decimal calculations for a Unit that is decimal-based.

But even using decimals, the current conversion strategy, which always has two steps: to a common unit, then to the desired unit, has issues. For example:

iex> to_microsecond = Decimal.new("1.0e-6")
#Decimal<0.0000010>
iex> to_minute = Decimal.new("1.6667e-8")  
#Decimal<1.6667E-8>
iex> Decimal.div(m, to_microsecond) |> Decimal.mult(to_minute)
#Decimal<480.00960>

So two thoughts:

  1. Add some additional direct conversion measures that are only a single calculation. Ie capture that seconds to minutes is 60.
  2. Apply rounding (which of course is already possible)

@LostKobrakai
Copy link
Contributor Author

@kipcole9 Maybe we can just flip the numbers from representing fractions of the smallest unit to be how many of the smallest unit fits into the current one: https://gist.github.com/LostKobrakai/4419bd3c245272e2fc04ef857c3e0f4a

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