wojtekmach / acme_bank

An example ☂ project
MIT License
756 stars 103 forks source link

Issue on bank_entries amount #15

Closed streamerd closed 6 years ago

streamerd commented 6 years ago

Amount shown for the bank_entries amount does not separate decimals from the amount being transferred. Have transferred "5.00" USD, parses well, however, it appears as "500" both in bank_entries table and the transaction log.

SET TRANSACTION ISOLATION LEVEL serializable []
[debug] QUERY OK db=2.9ms
INSERT INTO "bank_entries" ("account_id","amount","description","type","inserted_at","updated_at") VALUES ($1,$2,$3,$4,$5,$6) RETURNING "id" [3, {500, "USD"}, "pay back time. !!!", "debit", {{2018, 3, 2}, {4, 46, 1, 0}}, {{2018, 3, 2}, {4, 46, 1, 0}}]
[debug] QUERY OK db=4.3ms
INSERT INTO "bank_entries" ("account_id","amount","description","type","inserted_at","updated_at") VALUES ($1,$2,$3,$4,$5,$6) RETURNING "id" [1, {500, "USD"}, "pay back time. !!!", "credit", {{2018, 3, 2}, {4, 46, 1, 0}}, {{2018, 3, 2}, {4, 46, 1, 0}}]
[debug] QUERY OK source="bank_entries" db=3.7ms

Does it something to do with multiplying with 100 in the money.ex?

defp do_parse(sign, dollars, cents, currency) do sign = if sign == "-", do: -1, else: 1 cents = sign (String.to_integer(dollars) 100 + String.to_integer(cents)) {:ok, %Money{cents: cents, currency: currency}} end

wojtekmach commented 6 years ago

Thats by design, we store amount in cents as integers to make sure its exact value, however we display it as decimal number because thats what we are used to. Its a common practice in financial apps.

If I were to write it again Id probably use Decimal library to store arbitrary exact numbers.

streamerd commented 6 years ago

Thank you for the quick feedback. I'll be looking to Financial best practices a bit more.

wojtekmach commented 6 years ago

you're very welcome!