Class: Numeric

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

Instance Method Summary collapse

Instance Method Details

#frcObject



3
4
5
6
# File 'lib/numeric.rb', line 3

def frc
  f = BigDecimal(self.to_s) - BigDecimal(self.to_s).to_i
  return BigDecimal(f.to_s).to_s('F').to_f
end

#to_degObject



14
15
16
# File 'lib/numeric.rb', line 14

def to_deg
 self * 180 / Math::PI 
end

#to_gradObject



17
18
19
# File 'lib/numeric.rb', line 17

def to_grad
 self * 200 / Math::PI 
end

#to_num(n = 3, i = 2, g = 1, c = false, p = false) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/numeric.rb', line 21

def to_num(n=3, i=2, g=1, c=false, p=false)
  # n:  Threshold for when to show exponent
  # i:  Number og digits after the period
  # g:  Exponent grouping g=3 would always show exponent as a multiple of 3
  # c:  Use comma instead of period (European format)
  # p:  Separator per 3 digits (period if c=false, space when c=true)
  # e:  Exponent
  # ge: Grouped exponent
  # x:  Absolute value of number less exponent
  # s:  String to be displayed
  n = g if g > n
  self != 0 ? e = Math::log10(self.abs).floor : e = 0
  ge = g*(e/g).to_i
  x  = self.abs
  x  = (x / 10 ** ge) if e.abs >= n 
  s  = x.to_i.to_s
  self < 0 ? m = "-" : m  = ""
  s.sub!(/-/, '')
  f  = x.frc.to_s[0..(i + 1)].to_f
  f  = f.to_s.sub(/../, '')
  i > 0 ? s.sub!(/(\d*\.)(\d{,#{i}}).*/, '\1\2') : s.sub!(/(\d*).*/, '\1') 
  if e.abs >= n # If exponent kicks in
    s += "." + f.ljust(i, "0")
    s += "e"
    if ge < 0
      s += "-"
      s.sub!(/0\.(\d)/, '\1.')
    end
    ge = ge.abs
    s += "%02d" % [ge]
  else # No exponent
    o = "," if p and not c
    o = " " if p and c
    s.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{o}") if p
    s += "." + f.to_s.ljust(i, "0")
  end
  s.sub!(/\./, ',') if c
  return m + s
end

#to_radObject



8
9
10
# File 'lib/numeric.rb', line 8

def to_rad
 self * Math::PI / 180 
end

#to_radgObject



11
12
13
# File 'lib/numeric.rb', line 11

def to_radg
 self * Math::PI / 200 
end