Top Level Namespace

Defined Under Namespace

Classes: Milemarker

Instance Method Summary collapse

Instance Method Details

#ppnum(num, width = 0, decimals = 0) ⇒ String

WHAT? Defining a global function? Yup.

“Pretty print” a number into an underscore-delimited numeric string, right-space-padded out to the specified width (default 0 indicating “no padding”) and with the specified number of digits to the right of the decimal point (default again 0, meaning no decimal point at all)

Example: ppnum(10111) => “10_111”

ppnum(1234.56) => 1_235
ppnum(10111.3656, 10, 1) => "  10_111.4"

No attempt is made to deal gracefully with numbers that overrun the specified width

Parameters:

  • num (Numeric)

    the number to format

  • width (Integer) (defaults to: 0)

    The width to target

  • decimals (Integer) (defaults to: 0)

    Number of decimal places to show

Returns:

  • (String)

    The formatted number



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ppnum.rb', line 20

def ppnum(num, width = 0, decimals = 0)
  num = num.round(decimals)
  dec_str = if decimals.zero?
              ""
            else
              ".#{format("%.#{decimals}f", num).split(".").last}"
            end
  numstr = num.floor.to_s.reverse.split(/(...)/)
              .reject(&:empty?)
              .map(&:reverse)
              .reverse
              .join("_") + dec_str
  if width.zero?
    numstr
  else
    format "%#{width}s", numstr
  end
end