Class: FormatR::ScientificNotationFormatter

Inherits:
Formatter
  • Object
show all
Defined in:
lib/formatr.rb

Overview

make a formatter that will spit out scientific notation

Instance Method Summary collapse

Methods inherited from Formatter

#changeVarValue, #setVarValue

Constructor Details

#initialize(whole, radix, fraction, precision_g_e, exponent) ⇒ ScientificNotationFormatter

Make a new formatter that will print out in scientific notation



594
595
596
597
598
599
# File 'lib/formatr.rb', line 594

def initialize (whole, radix, fraction, precision_g_e, exponent)
  @total_size = ("@" + whole + radix + fraction + precision_g_e + exponent).size 
  @fraction = fraction.length
  @sig_figs = ("@" + whole + fraction).length
  @g_e = precision_g_e
end

Instance Method Details

#formatString(s, unused_var_name = nil, unused_aBinding = nil) ⇒ Object



601
602
603
604
605
606
# File 'lib/formatr.rb', line 601

def formatString (s, unused_var_name=nil, unused_aBinding=nil)
  #might want to put a %0 to pad w/ 0's
  precision = ((@g_e =~ /[Ee]/) ? @fraction : @sig_figs)
  result = sprintf("%#{@total_size}.#{precision}#{@g_e}", s)
  result
end