class Integer
increase Integer::LEN_STR_SIZE.
which should cover all practical numbers. If you need bigger numbers,
This class handles numbers between - 10 ** 10,000 and 10 ** 10,000
integer2.to_s_lex. (Similarly for “greater than” and “equals”.)
integer2, then integer1.to_s_lex is lexicographically less than
That is, if integer1 is less than integer2 for any two integers integer1 and
strings are structured so that lexicographic sorting order is preserved.
Provides support for converting integers to Strings, and back again. The
def to_s_lex
Convert the number to a lexicographically sortable string. This string will
def to_s_lex if (self >= 0) num_str = self.to_s len_str = LEN_STR_TEMPLATE % num_str.size return len_str + num_str else num = self * -1 num_str = num.to_s num_len = num_str.size len_str = LEN_STR_TEMPLATE % (NEG_LEN_MASK - num_len) num = (10 ** num_str.size) - num return "-#{len_str}%0#{num_len}d" % num end end
def to_s_pad(width = 10)
123.to_s_pad(5) => 00123 and -123.to_s_pad(5) => -0123
width:: number of characters in the string returned. Default is 10. So
positive and negative numbers you should use the Integer#to_s_lex method
direction as positive numbers. If you have very large numbers or a mix of
with negative numbers. That is negative numbers will sort in the opposite
accommodate all possible values. Also note that this method will not work
0s. You should make sure that you set the width to a number large enough to
Convert the number to a lexicographically sortable string by padding with
def to_s_pad(width = 10) "%#{width}d" % self end