Class Lisp::Format::Directives::GeneralFP
In: lib/facet/lisp-format.rb
Parent: Directive

Represents the ~G (General floating-point) directive. This outputs its argument using either a format like ~F or ~E depending upon given parameters and the magnitude of the argument.

Methods

execute  

Public Instance methods

Outputs the argument using exponential floating-point format. The algorithm to decide what format to use looks something like

  let
    10^n-1 <= arg < 10^n, or n = 0 if arg = 0,
    ee = e + 2, or ee = 4 if e undefined,
    ww = w - ee, or ww = nil if w undefined,
    dd = d - n, or dd = max(q, min(n, 7)) where q = number of
      characters necessary to print arg without loss of information
  in
    0 <= dd <= d, and print using
      ~ww,dd,,overflowchar,padcharF~ee@T
    or print using
      ~w,d,e,k,overflowchar,padchar,exponentcharG

Use of the @ modifier in the directives above depends on if it was passed to ~G in the first place.

The full form is

  ~w,d,e,k,overflowchar,padchar,exponentchar@G

with the following interpretations

w (nil)
if non-nil, the output will be exactly w characters long,
d (nil)
if non-nil, this is the number of digits output after the decimal point (.),
e (nil)
if non-nil, the exponent part of the output will be exactly e characters long,
k (1)
scaling factor - the number is first scaled using this value,
overflowchar (nil)
if non-nil, this character is used when this directive would produce output longer than that specified with the w directive,
padchar (?\s)
character to pad with if w is non-nil and output isn’t wide enough yet,
exponentchar (?e)
character to use for exponent divider,
@
numbers are always output with sign prepended.

An ArgumentError is raised if the argument is not a number or a string that can be converted to a number.

[Validate]