diff --git a/app/views/accounts/show.html.erb b/app/views/accounts/show.html.erb index cee93a47..4c69a5b3 100644 --- a/app/views/accounts/show.html.erb +++ b/app/views/accounts/show.html.erb @@ -11,7 +11,7 @@
Total Value
<%# TODO: Will need a better way to split a formatted monetary value into these 3 parts %>- <%= number_to_currency(@account.original_balance)[0] %> - <%= number_with_delimiter(@account.original_balance.round) %> - .<%= number_to_currency(@account.original_balance, precision: 2)[-2, 2] %> + <%= @account.original_currency.unit %> + <%= format_currency(@account.original_balance, precision: 0, unit: '') %> + <%- if @account.original_currency.precision.positive? -%> + <%= @account.original_currency.separator %><%= @account.original_balance.cents(precision: @account.original_currency.precision) %> + <% end %>
<% if @balance_series.nil? %>Data not available for the selected period
diff --git a/config/initializers/constants.rb b/config/initializers/constants.rb index 5f9790c8..1ec5f7ec 100644 --- a/config/initializers/constants.rb +++ b/config/initializers/constants.rb @@ -12,7 +12,7 @@ CURRENCY_OPTIONS = Hash.new { |hash, key| hash[key] = default_currency_options.d "NZD": { unit: "NZ$", precision: 2, delimiter: ",", separator: "." }, "AUD": { unit: "A$", precision: 2, delimiter: ",", separator: "." }, "KRW": { unit: "₩", precision: 0, delimiter: ",", separator: "." }, - "INR": { unit: "₹", precision: 0, delimiter: ",", separator: "." } + "INR": { unit: "₹", precision: 2, delimiter: ",", separator: "." } ) EXCHANGE_RATE_ENABLED = ENV["OPEN_EXCHANGE_APP_ID"].present? diff --git a/config/initializers/numeric_extensions.rb b/config/initializers/numeric_extensions.rb new file mode 100644 index 00000000..e86b21ad --- /dev/null +++ b/config/initializers/numeric_extensions.rb @@ -0,0 +1,11 @@ +class Numeric + def cents(precision: 2) + return "" unless precision.positive? + + cents = self.to_s.split(".")[1] + cents = "" unless cents.to_i.positive? + + zero_padded_cents = cents.ljust(precision, "0") + zero_padded_cents[0..precision - 1] + end +end diff --git a/config/initializers/string_extensions.rb b/config/initializers/string_extensions.rb new file mode 100644 index 00000000..83af5bec --- /dev/null +++ b/config/initializers/string_extensions.rb @@ -0,0 +1,13 @@ +class String + def unit + CURRENCY_OPTIONS[self.to_sym][:unit] + end + + def separator + CURRENCY_OPTIONS[self.to_sym][:separator] + end + + def precision + CURRENCY_OPTIONS[self.to_sym][:precision] + end +end diff --git a/test/initializers/numeric_extensions_test.rb b/test/initializers/numeric_extensions_test.rb new file mode 100644 index 00000000..6346761d --- /dev/null +++ b/test/initializers/numeric_extensions_test.rb @@ -0,0 +1,28 @@ +# test/initializers/big_decimal_extensions_test.rb +require "test_helper" + +class NumericExtensionsTest < ActiveSupport::TestCase + test "#cents returns the cents part with 2 precisions by default" do + amount = 123.45 + assert_equal "45", amount.cents + end + + test "#cents returns empty when precision is 0" do + amount = 123.45 + assert_equal "", amount.cents(precision: 0) + end + + test "#cents returns the cents part of the string with given precision" do + amount = 123.4862 + assert_equal "4", amount.cents(precision: 1) + assert_equal "486", amount.cents(precision: 3) + end + + test "#cents pads the cents part with zeros up to the specified precision" do + amount_without_decimal = 123 + amount_with_decimal = 123.4 + + assert_equal "00", amount_without_decimal.cents + assert_equal "40", amount_with_decimal.cents + end +end diff --git a/test/initializers/string_extensions_test.rb b/test/initializers/string_extensions_test.rb new file mode 100644 index 00000000..4812fe98 --- /dev/null +++ b/test/initializers/string_extensions_test.rb @@ -0,0 +1,19 @@ +# test/string_extensions_test.rb +require "test_helper" + +class StringExtensionsTest < ActiveSupport::TestCase + test "#unit returns the currency unit for a given currency code" do + assert_equal "$", "USD".unit + assert_equal "€", "EUR".unit + end + + test "#separator returns the currency separator for a given currency code" do + assert_equal ".", "USD".separator + assert_equal ",", "EUR".separator + end + + test "#precision returns the currency's precision for a given currency code" do + assert_equal 2, "USD".precision + assert_equal 0, "KRW".precision + end +end