Class: Attio::Util::CurrencyFormatter
- Inherits:
 - 
      Object
      
        
- Object
 - Attio::Util::CurrencyFormatter
 
 
- Defined in:
 - lib/attio/util/currency_formatter.rb
 
Overview
Utility class for formatting currency amounts
Constant Summary collapse
- CURRENCY_SYMBOLS =
          
Map of currency codes to their symbols
 { "USD" => "$", "EUR" => "€", "GBP" => "£", "JPY" => "¥", "CNY" => "¥", "INR" => "₹", "KRW" => "₩", "CAD" => "$", "AUD" => "$", "CHF" => "CHF ", "SEK" => "SEK ", "NOK" => "NOK ", "DKK" => "DKK ", "PLN" => "zł", "BRL" => "R$", "MXN" => "$", "NZD" => "$", "SGD" => "$", "HKD" => "$", "ZAR" => "R", "THB" => "฿", "PHP" => "₱", "IDR" => "Rp", "MYR" => "RM", "VND" => "₫", "TRY" => "₺", "RUB" => "₽", "UAH" => "₴", "ILS" => "₪", "AED" => "د.إ", "SAR" => "﷼", "CLP" => "$", "COP" => "$", "PEN" => "S/", "ARS" => "$" }.freeze
- NO_DECIMAL_CURRENCIES =
          
Currencies that typically don't use decimal places
 %w[JPY KRW VND IDR CLP].freeze
Class Method Summary collapse
- 
  
    
      .decimal_places_for(currency_code)  ⇒ Integer 
    
    
  
  
  
  
  
  
  
  
  
    
Determine the number of decimal places for a currency.
 - 
  
    
      .format(amount, currency_code = "USD", options = {})  ⇒ String 
    
    
  
  
  
  
  
  
  
  
  
    
Format an amount with the appropriate currency symbol.
 - 
  
    
      .format_number(amount, currency_code = "USD", options = {})  ⇒ String 
    
    
  
  
  
  
  
  
  
  
  
    
Format just the numeric part without currency symbol.
 - 
  
    
      .symbol_for(currency_code)  ⇒ String 
    
    
  
  
  
  
  
  
  
  
  
    
Get the currency symbol for a given code.
 - 
  
    
      .uses_decimals?(currency_code)  ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    
Check if a currency typically uses decimal places.
 
Class Method Details
.decimal_places_for(currency_code) ⇒ Integer
Determine the number of decimal places for a currency
      108 109 110 111  | 
    
      # File 'lib/attio/util/currency_formatter.rb', line 108 def decimal_places_for(currency_code) currency_code = currency_code.to_s.upcase NO_DECIMAL_CURRENCIES.include?(currency_code) ? 0 : 2 end  | 
  
.format(amount, currency_code = "USD", options = {}) ⇒ String
Format an amount with the appropriate currency symbol
      58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95  | 
    
      # File 'lib/attio/util/currency_formatter.rb', line 58 def format(amount, currency_code = "USD", = {}) currency_code = currency_code.to_s.upcase symbol = symbol_for(currency_code) # Determine decimal places decimal_places = [:decimal_places] || decimal_places_for(currency_code) thousands_sep = [:thousands_separator] || "," decimal_sep = [: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  | 
  
.format_number(amount, currency_code = "USD", options = {}) ⇒ String
Format just the numeric part without currency symbol
      125 126 127 128 129  | 
    
      # File 'lib/attio/util/currency_formatter.rb', line 125 def format_number(amount, currency_code = "USD", = {}) result = format(amount, currency_code, ) symbol = symbol_for(currency_code) result.sub(/^#{Regexp.escape(symbol)}/, "") end  | 
  
.symbol_for(currency_code) ⇒ String
Get the currency symbol for a given code
      100 101 102 103  | 
    
      # File 'lib/attio/util/currency_formatter.rb', line 100 def symbol_for(currency_code) currency_code = currency_code.to_s.upcase CURRENCY_SYMBOLS[currency_code] || "#{currency_code} " end  | 
  
.uses_decimals?(currency_code) ⇒ Boolean
Check if a currency typically uses decimal places
      116 117 118  | 
    
      # File 'lib/attio/util/currency_formatter.rb', line 116 def uses_decimals?(currency_code) decimal_places_for(currency_code) > 0 end  |