class Attio::Util::CurrencyFormatter

def format(amount, currency_code = "USD", options = {})

Returns:
  • (String) - The formatted currency string

Options Hash: (**options)
  • :decimal_separator (String) -- Character for decimal separation (default: ".")
  • :thousands_separator (String) -- Character for thousands separation (default: ",")
  • :decimal_places (Integer) -- Number of decimal places (auto-determined by default)

Parameters:
  • options (Hash) -- Formatting options
  • currency_code (String) -- The ISO 4217 currency code
  • amount (Numeric) -- The amount to format
def format(amount, currency_code = "USD", options = {})
  currency_code = currency_code.to_s.upcase
  symbol = symbol_for(currency_code)
  
  # Determine decimal places
  decimal_places = options[:decimal_places] || decimal_places_for(currency_code)
  thousands_sep = options[:thousands_separator] || ","
  decimal_sep = options[:decimal_separator] || "."
  
  # Handle zero amounts
  if amount == 0
    if decimal_places > 0
      return "#{symbol}0#{decimal_sep}#{"0" * decimal_places}"
    else
      return "#{symbol}0"
    end
  end
  
  # Handle negative amounts
  negative = amount < 0
  abs_amount = amount.abs
  
  # Format the amount
  if decimal_places == 0
    # No decimal places
    formatted = format_with_separators(abs_amount.to_i, thousands_sep)
    formatted = "-#{formatted}" if negative
    "#{symbol}#{formatted}"
  else
    # With decimal places
    whole = abs_amount.to_i
    decimal = ((abs_amount - whole) * (10 ** decimal_places)).round
    formatted_whole = format_with_separators(whole, thousands_sep)
    formatted_whole = "-#{formatted_whole}" if negative
    formatted_decimal = decimal.to_s.rjust(decimal_places, "0")
    "#{symbol}#{formatted_whole}#{decimal_sep}#{formatted_decimal}"
  end
end