Sha256: d967edc8eb7b0c5cfaba9d7e04c2940a37c81ee68b98c742e0e15e3acd322517

Contents?: true

Size: 497 Bytes

Versions: 2

Compression:

Stored size: 497 Bytes

Contents

# frozen_string_literal: true

# Redefine float with the apropriate options
class Float
  def sigfig(digits)
    f = format("%.#{digits - 1}e", self)
    f = f.to_f
    i = f.to_i
    (i == f ? i : f).to_s
  end

  def hisigfig(digits)
    format("%.#{digits - 1}e", self).split('e')[0].delete('.')
  end
end

# Redefine Integer class to include the same methods as Float
class Integer
  def sigfig(digits)
    to_f.sigfig(digits)
  end

  def hisigfig(digits)
    to_f.hisigfig(digits)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pdground-0.1.4 lib/pdground/extension.rb
pdground-0.1.3 lib/pdground/extension.rb