Coder Social home page Coder Social logo

yepesasecas / currency_formatter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from oldhammade/currency_formatter

0.0 1.0 0.0 397 KB

An Elixir function to format a number to a currency using ISO standards

License: Do What The F*ck You Want To Public License

Elixir 63.48% HTML 36.52%

currency_formatter's Introduction

CurrencyFormatter

This package offers an Elixir function to format a number to a currency using ISO standards.

The JSON iso data has been gracefully borrowed from the ruby money gem.

Examples :

Formatting cents to currency string

iex> CurrencyFormatter.format(654321, :eur)
"€6.543,21"

iex> CurrencyFormatter.format(654300, :eur)
"€6.543"

iex> CurrencyFormatter.format(654300, :eur, keep_decimals: true)
"€6.543,00"

iex> CurrencyFormatter.format(1, "huf")
"0,01Ft"

iex> CurrencyFormatter.format(123456)
"$1,234.56"

iex> CurrencyFormatter.format(654321, "AUD")
"$6,543.21"

iex> CurrencyFormatter.format(123456, disambiguate: true)
"US$1,234.56"

iex> CurrencyFormatter.format(654321, "AUD", disambiguate: true)
"A$6,543.21"

Formatting cents to a currency raw html string

iex> CurrencyFormatter.raw_html_format(123456, "EUR")
     ~s[<span class="currency-formatter-symbol">€</span><span class="currency-formatter-amount">1.234,56</span>]

Formatting cents to a currency safe(phoenix) html string

iex> CurrencyFormatter.html_format(123456, "EUR")
     {:safe,
       [60, "span", 32, "class", 61, 34, "currency-formatter-symbol", 34,
        62, "€", 60, 47, "span", 62, 60, "span", 32, "class", 61, 34,
        "currency-formatter-amount", 34, 62, "1.234,56", 60, 47, "span",
       62]}

Requesting formatting instructions for a currency

iex> CurrencyFormatter.instructions(:EUR)
%{"alternate_symbols" => [], "decimal_mark" => ",", "html_entity" => "&#x20AC;",
  "iso_code" => "EUR", "iso_numeric" => "978", "name" => "Euro", "priority" => 2,
  "smallest_denomination" => 1, "subunit" => "Cent", "subunit_to_unit" => 100,
  "symbol" => "€", "symbol_first" => true, "thousands_separator" => "."}

Get a map of all currencies and their instructions

iex> currencies = CurrencyFormatter.get_currencies()
iex> Enum.count(currencies)
172
iex> currencies["usd"]
%{"alternate_symbols" => ["US$"], "decimal_mark" => ".",
  "disambiguate_symbol" => "US$", "html_entity" => "$", "iso_code" => "USD",
  "iso_numeric" => "840", "name" => "United States Dollar", "priority" => 1,
  "smallest_denomination" => 1, "subunit" => "Cent", "subunit_to_unit" => 100,
  "symbol" => "$", "symbol_first" => true, "thousands_separator" => ","}

Getting a list of tuples for use with a select dropdown

iex> CurrencyFormatter.get_currencies_for_select()
["AED", "AFN", "ALL", ...]
iex> CurrencyFormatter.get_currencies_for_select(:names)
[{"AED", "United Arab Emirates Dirham"}, {"AFN", "Afghan Afghani"} , {"ALL", "Albanian Lek"}, ...]
iex> CurrencyFormatter.get_currencies_for_select(:symbols)
[{"AED", "د.إ"}, {"AFN", "؋"}, {"ALL", "L"}, ...]
iex> CurrencyFormatter.get_currencies_for_select(:disambiguate_symbols)
[[{"AED", "د.إ"}, {"AFN", "؋"}, {"ALL", "Lek"}, ...]

Get the disambiguous symbol of a currrency

iex> CurrencyFormatter.symbol(:AUD)
"A$"

Installation

As this is available in Hex, the package can be installed as:

  1. Add currency_formatter to your list of dependencies in mix.exs:
def deps do
  [{:currency_formatter, "~> 0.9.0"}]
end

Setup

By default you will have 172 currencies available, there are two configuration options that can be set in your app's config.exs file to limit the number of currencies.

If you would like to limit the list to only include specific currencies, you can configure a whitelist by passing a list of ISO codes to include:

config :currency_formatter, :whitelist, ["EUR", "GBP", "USD"]

To exclude certain currencies from the list, you can configure a blacklist using a list of ISO codes to exclude:

config :currency_formatter, :blacklist, ["XDR", "XAG", "XAU"]

Documentation

API documentation is available at https://hexdocs.pm/currency_formatter and http://smeevil.github.io/currency_formatter

currency_formatter's People

Contributors

duff avatar jknipp avatar marceldegraaf avatar minibikini avatar oldhammade avatar optikfluffel avatar smeevil avatar yepesasecas avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.