Module: Calc::ResultFormatter

Defined in:
lib/calc/result_formatter.rb

Overview

Formats results from Calculator#process for display to the end user.

Instance Method Summary collapse

Instance Method Details

#process(input:, places: TEN_THOUSANDTHS) ⇒ String

Returns unrounded, output-ready version of result.

Parameters:

  • places (Integer) (defaults to: TEN_THOUSANDTHS)

    the number of decimal places to truncate fractional result to

  • input (BigDecimal, String)

    number to perform an Operation on or the sign of the Operation to perform

Returns:

  • (String)

    unrounded, output-ready version of result

Raises:

  • (TooFewElementsOnStackError)

    if an operation is attempted and there are not enough elements on the stack


15
16
17
18
19
20
# File 'lib/calc/result_formatter.rb', line 15

def process(input:, places: TEN_THOUSANDTHS)
  result = super(input: input)

  return result.truncate(places).to_s('F') if fractional?(result)
  result.truncate.to_s
end