Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/kannum.rb,
lib/digit_delim.rb

Instance Method Summary collapse

Instance Method Details

#str_to_numObject



304
# File 'lib/kannum.rb', line 304

def str_to_num; KanNum.str_to_num self; end

#to_fuObject

:method: to_fu convert digit-string into float concerning with unit.

Args

none.

Return

A number in Float. If the original string has ‘%’, the number is divided by 100.0.



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/digit_delim.rb', line 61

def to_fu
  ret = self.to_wof
  # if self =~ /%$/ then ret /= 100.0 else ret end
  case self
  when /%$/
    ret /= 100.0

  else
    ret

  end

end

#to_wofObject

:method: to_wof remove delimiters in digit-string and convert it to Float.

Args

none.

Return

A number in Float.



52
# File 'lib/digit_delim.rb', line 52

def to_wof; self.wo_dm.to_f_old; end

#w_dm(adp = -1,, n = 3) ⇒ Object

:method: w_dm with delimiters: separate digit-string into each n-digits with delimiters.

Args

adp

length of digits after the decimal point. (-1 if you want all of digits after the decimal point)

n

the number of digits length of each separated string.

Return

separated string.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/digit_delim.rb', line 22

def w_dm(adp=-1, n=3)
  self.gsub( /(\d+)?(\.\d*)?/ ) {
    s1="#{$1}".dup; s2="#{$2}".dup;

    if s2.size != 0
      if adp == -1
        s2="#{s2}00".slice(0, s2.size)
      else
        s2="#{s2}00".slice(0, adp+1)
      end
    end
    s1.reverse.gsub( /\d{#{n}}(?=\d)/ ){|r| r+","}.reverse+s2
  }
end

#wo_dmObject

:method: wo_dm without delimiters: remove the delimiters in digit-string.

Args

none.

Return

digit-string without the delimiters.



44
# File 'lib/digit_delim.rb', line 44

def wo_dm; self.gsub(/,/, ""); end