Sha256: e7bbf898b71d8ce83b0062736941f7666250228ca07fb318a40c4b28144eec95

Contents?: true

Size: 1.51 KB

Versions: 12

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

require "spout/helpers/number_helper"

module Spout
  module Helpers
    # Formats numbers in coverage and outlier tables.
    class TableFormatting
      # def initialize(number)
      #   @number = number
      # end

      # type:  :count    or   :decimal
      def self.format_number(number, type, format = nil)
        if number.nil?
          format_nil
        elsif type == :count
          format_count(number)
        else
          format_decimal(number, format)
        end
      end

      def self.format_nil
        "-"
      end

      #   count:
      #        0          ->             "-"
      #       10          ->            "10"
      #     1000          ->         "1,000"
      # Input (Numeric)   -> Output (String)
      def self.format_count(number)
        number.zero? || number.nil? ? "-" : Spout::Helpers::NumberHelper.number_with_delimiter(number)
      end

      # decimal:
      #        0          ->           "0.0"
      #       10          ->          "10.0"
      #      -50.2555     ->         "-50.3"
      #     1000          ->       "1,000.0"
      # 12412423.42252525 ->  "12,412,423.4"
      # Input (Numeric)   -> Output (String)
      def self.format_decimal(number, format)
        precision = 1
        precision = -Math.log10(number.abs).floor if number.abs < 1.0 && !number.zero?

        number = Spout::Helpers::NumberHelper.number_with_delimiter(number.to_f.round(precision))
        number = format % number if format
        number
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
spout-1.0.0 lib/spout/helpers/table_formatting.rb
spout-1.0.0.beta3 lib/spout/helpers/table_formatting.rb
spout-1.0.0.beta2 lib/spout/helpers/table_formatting.rb
spout-1.0.0.beta1 lib/spout/helpers/table_formatting.rb
spout-0.14.1 lib/spout/helpers/table_formatting.rb
spout-0.14.0 lib/spout/helpers/table_formatting.rb
spout-0.14.0.rc lib/spout/helpers/table_formatting.rb
spout-0.14.0.beta3 lib/spout/helpers/table_formatting.rb
spout-0.14.0.beta2 lib/spout/helpers/table_formatting.rb
spout-0.14.0.beta1 lib/spout/helpers/table_formatting.rb
spout-0.13.0 lib/spout/helpers/table_formatting.rb
spout-0.13.0.beta2 lib/spout/helpers/table_formatting.rb