sergio

sergio

How can I customize the symbol for a currency in the `money` package?

config :money,
  symbol_space: true

image

That $b symbol may be the ISO currency symbol for the Bolivian “Boliviano” but we’re used to seeing prices like this: Bs. 150 or Bs. 49.89

How can I customize the symbol for this specific currency in the Money package?

Marked As Solved

sergio

sergio

I added these simple functions to my helper file:

def format_money(%{amount: amount, currency: :BOB}) do
  "Bs. #{amount}"
end

def format_money(money) do
  money
end

Also Liked

pdgonzalez872

pdgonzalez872

Nice, that’s a fair approach! I was curious and wanted to see what it would look like to use config like the docs suggest.

TLDR: create a custom currency since the display seems to be different than the other BOL options (money/lib/money/currency.ex at master · elixirmoney/money · GitHub).

If you are past the TLDR, after creating a new mix project, this diff does the job:

commit 2c545bd7df55b4eff0ae889937a5640647736bd2
Author: Paulo Daniel Gonzalez <pdgonzalez872@email.com>
Date:   Sun Jan 7 13:31:48 2024 -0600

    Solve https://forum.elixirforum.net/t/how-can-i-customize-the-symbol-for-a-currency-in-the-money-package/60825

diff --git a/config/config.exs b/config/config.exs
new file mode 100644
index 0000000..5df6358
--- /dev/null
+++ b/config/config.exs
@@ -0,0 +1,19 @@
+import Config
+
+config :money,
+  default_currency: :BOV2,
+  separator: ",",
+  delimiter: ".",
+  symbol: true,
+  symbol_on_right: false,
+  symbol_space: true,
+  fractional_unit: true,
+  strip_insignificant_zeros: false,
+  code: false,
+  minus_sign_first: true,
+  strip_insignificant_fractional_unit: false,
+  custom_currencies: [
+    BOV2: %{name: "BolivianoBs", symbol: "Bs.", exponent: 2}
+  ]
+
+import_config "#{config_env()}.exs"
diff --git a/config/dev.exs b/config/dev.exs
new file mode 100644
index 0000000..e69de29
diff --git a/config/test.exs b/config/test.exs
new file mode 100644
index 0000000..e69de29
diff --git a/mix.exs b/mix.exs
index 79efc4f..cffe122 100644
--- a/mix.exs
+++ b/mix.exs
@@ -21,6 +21,7 @@ defmodule MoneyForum.MixProject do
   # Run "mix help deps" to learn about dependencies.
   defp deps do
     [
+      {:money, "~> 1.12"}
       # {:dep_from_hexpm, "~> 0.3.0"},
       # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
     ]
diff --git a/mix.lock b/mix.lock
new file mode 100644
index 0000000..a043fc3
--- /dev/null
+++ b/mix.lock
@@ -0,0 +1,3 @@
+%{
+  "money": {:hex, :money, "1.12.4", "9d9817aa79d1317871f6b006721c264bf1910fb28ba2af50746514f0d7e8ddbe", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}, {:ecto, "~> 1.0 or ~> 2.0 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}, {:phoenix_html, "~> 2.0 or ~> 3.0 or ~> 4.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}], "hexpm", "87e4bb907df1da184cb4640569d8df99ee6d88c84ce4f5da03cb2fab8d433eb9"},
+}
diff --git a/test/money_forum_test.exs b/test/money_forum_test.exs
index 5ab245a..e9d417b 100644
--- a/test/money_forum_test.exs
+++ b/test/money_forum_test.exs
@@ -3,6 +3,8 @@ defmodule MoneyForumTest do
   doctest MoneyForum
 
   test "greets the world" do
-    assert MoneyForum.hello() == :world
+    assert %Money{amount: 123_450, currency: :BOV2} = bol2 = Money.new(1_234_50, :BOV2)
+    assert "Bs." = Money.Currency.symbol(bol2)
+    assert "Bs. 1,234.50" = Money.to_string(bol2)
   end
 end
LostKobrakai

LostKobrakai

That‘s why I generally suggest ex_money over money. It comes not just with a type, but e.g. also knowledge about how formatting should work in different places of the world.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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

Other popular topics Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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

We're in Beta

About us Mission Statement