Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alphanumeric formatter does not work with accents #24

Open
diegoesp opened this issue Oct 14, 2017 · 0 comments
Open

Alphanumeric formatter does not work with accents #24

diegoesp opened this issue Oct 14, 2017 · 0 comments

Comments

@diegoesp
Copy link

Hi,

given the use of bytesize, accents confuse the algorithm.

Below an implementation that does not break positioting:

module Alphanumeric
  def format_alphanumeric(input, byte_width)
    input = input.to_s
    String.new(input).ljust(byte_width, " ")
  end
end

I will also paste all my formatters below in case someone may need them.

module Fixy
  module Formatter

    # Justifies an alpha to the left
    module Alphanumeric
      def format_alphanumeric(input, byte_width)
        input = input.to_s
        String.new(input).ljust(byte_width, " ")
      end
    end

    # Formats a number 93 to "00000093"
    module Numeric
      def format_numeric(input, byte_width)
        String.new(input.to_s).rjust(byte_width, "0")
      end
    end

    # Formats datetime to YYYYMMDDHHMMSS
    module DateTime
      def format_datetime(input, byte_width)
        input.strftime("%Y%m%d%H%M%S")
      end
    end

    # Formats date to YYYYMMDD
    module Date
      def format_date(input, byte_width)
        ::Date.strptime(input, "%d/%m/%Y").strftime("%Y%m%d")
      end
    end

    # Formats date to YYMMDD
    module ShortDate
      def format_short_date(input, byte_width)
        ::Date.strptime(input, "%d/%m/%Y").strftime("%y%m%d")
      end
    end

    # Formats floating point "9.34" to "000000934"
    module Amount
      def format_amount(input, byte_width)
         input = "%0.2f" % input
         input.gsub(".", "").rjust(byte_width, "0")
      end
    end

  end
end
@diegoesp diegoesp changed the title Alphanueric formatter does not work with accents Alphanumeric formatter does not work with accents Dec 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant