class ICU::NumberFormatting::CurrencyFormatter

Public Class Methods

new(locale, style = :default) click to toggle source
# File lib/ffi-icu/number_formatting.rb, line 95
def initialize(locale, style = :default)
  if %w(iso plural).include?((style || '').to_s)
    if Lib.version.to_a.first >= 53
      style = "currency_#{style}".to_sym
    else
      fail "Your version of ICU (#{Lib.version.to_a.join('.')}) does not support #{style} currency formatting (supported only in version >= 53)"
    end
  elsif style && style.to_sym != :default
    fail "The ffi-icu ruby gem does not support :#{default} currency formatting (only :default, :iso, and :plural)"
  else
    style = :currency
  end
  @f = make_formatter(style, locale)
end

Public Instance Methods

format(number, currency) click to toggle source
# File lib/ffi-icu/number_formatting.rb, line 110
def format(number, currency)
  needed_length = 0
  out_ptr = UCharPointer.new(needed_length)

  retried = false

  begin
    Lib.check_error do |error|
      needed_length = Lib.unum_format_currency(@f, number, UCharPointer.from_string(currency, 4), out_ptr, needed_length, nil, error)
    end
    out_ptr.string
  rescue BufferOverflowError
    raise BufferOverflowError, "needed: #{needed_length}" if retried
    out_ptr = out_ptr.resized_to needed_length
    retried = true
    retry
  end
end