kip

kip

ex_cldr Core Team

Ex_money - money with currency type

ex_money is a package to manage a money data type and provide localised formatting, arithmetic, exchange rates, basic financial calculations and serialization with a special attention on preserving precision.

CLDR data drives the localisation capabilities. As a result ex_money knows about each currencies precision, separators, grouping and symbols in any of about 500 locales.

Version 3.2.4 is out this week with a new ability to parse money strings. Some examples follow:

  # These are the strings available for a given currency
  # and locale that are recognised during parsing
  iex> Cldr.Currency.strings_for_currency :AUD, "de"
  ["aud", "au$", "australischer dollar", "australische dollar"]

  # Parsing can be localised
  iex> Money.parse "12 346 dollar australien", locale: "fr"
  #Money<:AUD, 12346>

  iex> Money.parse "A$ 12346", locale: "en"
  #Money<:AUD, 12346>

  # Note that the decimal separator in the "de" locale
  # is a `.`
  iex> Money.parse "AU$ 12346,45", locale: "de"
  #Money<:AUD, 12346.45>

  # Round trip formatting is supported
  iex> {:ok, string} = Cldr.Number.to_string 1234, Money.Cldr, currency: :AUD
  {:ok, "A$1,234.00"}
  iex> Money.parse string
  #Money<:AUD, 1234.00>

  # Fuzzy matching is possible
  iex> Money.parse("100 eurosports", fuzzy: 0.8)
  #Money<:EUR, 100>

  iex> Money.parse("100 eurosports", fuzzy: 0.9)
  {:error,
   {Money.Invalid, "Unable to create money from \"eurosports\" and \"100\""}}

  # Eligible currencies can be filtered by type
  iex> Money.parse("100 eurosports", fuzzy: 0.8, currency_filter: [:current, :tender])
  #Money<:EUR, 100>

Most Liked

kip

kip

ex_cldr Core Team

Finally. finally. ex_money is updated to be based upon localize, not ex_cldr.

Updated libraries

I’m definitely looking for any feedback on ex_money before I publish the final 6.0 release.

Changes

Overall there are no functional changes, only platform changes:

  • add localize
  • remove ex_cldr
  • move all custom and private currency registry functions to Money.Currency. They were removed from Localize.Currency. This is a much cleaner responsibility model.
  • remove gringotts optional dependency
  • remove :json_library configuration (standardise on :json)
  • requires at least Elixir 1.17
kip

kip

ex_cldr Core Team

ex_money just hit a million downloads on hex.pm. Small beer in comparison to its older sibling money but still - thank you to everyone who has found value in it and given it your support.

First commit was June 19, 2016 so it’s been seven years in the making :slight_smile: The last release was 3 weeks ago and there have been no issues raised since March. So its in good shape, still under active support and feature requests are always welcome.

kip

kip

ex_cldr Core Team

I’ve just published the first in a new series of libraries focused on input. First up is ex_money_input. Soon to come is one for plain numbers (which will be part of a new library, localize_inputs).

It’s the form-input layer for the elixir-money family. It rounds out the trio: ex_money for the data type, ex_money_sql for Ecto persistence, and now ex_money_input for users actually typing a value into a form.

Example component usage

<.money_input
  form={@form}
  field={:price}
  default_currency={:USD}
  currency_picker={true}
  preferred_currencies={[:USD, :EUR, :GBP, :JPY]}
/>

Features

  • A <.money_input> HEEx component with a paired <.currency_picker>. Locale determines the separators, symbol position, fractional digits (USD 2, JPY 0, BHD 3), and even the digit system (Arabic, Persian).
  • A small JS hook wrapping AutoNumeric (MIT, Alexandre Bonneau) for live formatting and cursor preservation. Optional — without it the input still works server-side. But AutoNumeric does a great job on the UX side, respecting and understanding different locale grouping and fraction separators.
  • An Ecto changeset bridge (cast_money/3, validate_money/3) with currency-aware precision and bounds.
  • A headless cast/validate/currency layer that’s usable without Phoenix.

Money Input Playground

There’s a Plug-based visualizer that demos every component across CLDR locales and currencies.

It’s deployed at https://elixir-money-input.fly.dev so you can explore its potential use for your projects.

Links

Docs: https://hexdocs.pm/ex_money_input
Source: https://github.com/ex-money/money_input
Integration guide: https://hexdocs.pm/ex_money_input/integration.html

Feedback welcome — particularly on the locales you actually ship to. The CLDR coverage is wide but there’s always a corner case.

Where Next?

Popular in Announcing Top

Crowdhailer
I have been updating a library that allows you to pipe between functions that use the erlang result tuple convention. Assuming you have ...
New
riverrun
I’ve just released version 3 of Comeonin, a password hashing library. The following small changes have been made: changes to the NIF c...
New
Eiji
ExApi is a library that I’m developing now and hope release soon This library will allow to: list all apis list all api implementation...
New
kip
ex_cldr provides localisation and internationalisation support based upon the data from the Unicode CLDR project. Unicode released CLDR ...
407 13014 120
New
ityonemo
Currently just starting out on a new mini-project - getting zig NIFs to run in elixir. https://github.com/ityonemo/zigler The idea here...
New
Qqwy
TypeCheck: Fast and flexible runtime type-checking for your Elixir projects. Core ideas Type- and function specifications are const...
336 14482 100
New
New
kip
Image is an image processing library for Elixir. It is based upon the fabulous vix library that provides a libvips wrapper for Elixir. I...
622 18934 194
New
trisolaran
Hi! :waving_hand: I would like to present LiveSelect, a little library that I wrote to easily add a dynamic selection input to your LV f...
198 10954 107
New
woutdp
Hi! I wanted to introduce my latest project LiveSvelte. It allows you to render Svelte inside LiveView with end-to-end reactivity. It’s ...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement