Class: Lisp::Format::Directives::DollarFP

Inherits:
Directive show all
Defined in:
lib/carat/lisp-format.rb

Overview

Represents the ~$ (Dollars floating-point) directive. This directive outputs a floating point argument

Instance Attribute Summary

Attributes inherited from Directive

#pos

Instance Method Summary collapse

Methods inherited from Directive

#initialize, #join

Constructor Details

This class inherits a constructor from Lisp::Format::Directives::Directive

Instance Method Details

#execute(state) ⇒ Object

Outputs the argument using a floating point format that suits dollar values. The full form is

~d,n,w,padchar:@$

with the following interpretations

d (2)

number of digits to print after the decimal point (.),

n (1)

number of digits to print before the decimal point (.),

w (0)

minimum width of the field,

padchar (?s)

character used to produce right-adjusting padding with,

:

the sign of the value is output before any padding,

@

numbers are always output with sign prepended.



1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
# File 'lib/carat/lisp-format.rb', line 1237

def execute(state)
  digits = param(0, state, 2)
  idigits = param(1, state, 1)
  width = param(2, state, 0)
  padchar = param(3, state, ?\s)
  arg = state.next_arg
  if arg.respond_to :to_int
    sign = (arg >= 0 ? (at_mod? ? '+' : '') : '-')
    str = sprintf("%0#{idigits + digits + 1}.#{digits}f", arg.abs)
    if colon_mod?
      str = sign + str.rjust(width, padchar.chr)
    else
      str = (sign + str).rjust(width, padchar.chr)
    end
    state.output str
  elsif arg.respond_to? :to_i
    state.push_back_arg
    parameters = @params[2].nil? ? [] : [@params[2]]
    Factory.build(?D, parameters, [], nil, @pos).execute(state)
  else
    arg_error 'argument is not a number or a number string'
  end
end