Skip to content

Conversation

@astery
Copy link

@astery astery commented Jun 11, 2024

Removes &to_string/1 in &number_to_delimited/1 because it's a reason for a failure of custom types what implement Number.Conversion conversion protocol but do not implement String.Chars (or implements it not the format that Decimal expects). And simply there is no need in it at all.

But the &number_to_phone/1 kept untouched because it heavily relies on string representation and if someone introduce a special type for the phone number representation it's doubtful that he would use any other functions from the package for that type.

astery added 2 commits June 11, 2024 13:18
Removes &to_string/1 in &number_to_delimited/1 because it's a reason for
a failure of custom types what implement `Number.Conversion` conversion
protocol but do not implement `String.Chars` (or implements it not the
format that Decimal expects). And simply there is no need in it at all.

But the &number_to_phone/1 kept untouched because it heavily relies on
string representation and if someone introduce a special type for
the phone number representation it's doubtful that he would use any other
functions from the package for that type.
@astery
Copy link
Author

astery commented Jun 11, 2024

My initial need was to format monetary type from money package (I know it has it's own functions to format) via &number_to_delimited/2.

defimpl Number.Conversion, for: Money do
  @moduledoc false

  def to_float(value) do
    value
    |> Money.to_decimal()
    |> Decimal.to_float()
  end

  def to_decimal(value) do
    Money.to_decimal(value)
  end
end

Such call raises exception:

iex(2)> "1000" |> Money.parse!() |> Number.Delimit.number_to_delimited()
** (Decimal.Error) : number parsing syntax: "1 000,00"
    (decimal 2.1.1) lib/decimal.ex:1205: Decimal.new/1
    (number 1.0.3) lib/number/delimit.ex:104: Number.Delimit.number_to_delimited/2
    iex:2: (file)

This PR fixes this issue.

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

Successfully merging this pull request may close these issues.

1 participant