1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-19 13:19:39 +02:00
Maybe/lib/money/formatting.rb
Zach Gollwitzer a3cd5f4f1d
Some checks failed
Publish Docker image / ci (push) Has been cancelled
Publish Docker image / Build docker image (push) Has been cancelled
Format money for trade history in holdings drawer (#1961)
* Format money for trade history in holdings drawer

* Fix broken tests

* Lint fix
2025-03-07 19:09:54 -05:00

43 lines
1.1 KiB
Ruby

module Money::Formatting
include ActiveSupport::NumberHelper
def format(options = {})
locale = options[:locale] || I18n.locale
default_opts = format_options(locale)
number_to_currency(amount, default_opts.merge(options))
end
alias_method :to_s, :format
def format_options(locale = nil)
local_option_overrides = locale_options(locale)
{
unit: get_symbol,
precision: currency.default_precision,
delimiter: currency.delimiter,
separator: currency.separator,
format: currency.default_format
}.merge(local_option_overrides)
end
private
def get_symbol
if currency.symbol == "$" && currency.iso_code != "USD"
[ currency.iso_code.first(2), currency.symbol ].join
else
currency.symbol
end
end
def locale_options(locale)
case [ currency.iso_code, locale.to_sym ]
when [ "EUR", :nl ], [ "EUR", :pt ]
{ delimiter: ".", separator: ",", format: "%u %n" }
when [ "EUR", :en ], [ "EUR", :en_IE ]
{ delimiter: ",", separator: "." }
else
{}
end
end
end