Module: CoreExtend::FloatModule

Included in:
BigDecimal, Float, Integer
Defined in:
lib/core_extend/float_module.rb

Instance Method Summary collapse

Instance Method Details

#decimal_point(point = 2) ⇒ Object

保留小数点位数



5
6
7
8
# File 'lib/core_extend/float_module.rb', line 5

def decimal_point(point=2)
  return sprintf("%0.#{point}f", self).to_f if point>0
  self.to_i
end

#to_money(type = 'num', point = 2) ⇒ Object

显示成钱数 ===Parameters type enum 'num','text' ===Returns string



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/core_extend/float_module.rb', line 15

def to_money(type='num', point=2)
  str = decimal_point(point).to_s
  int_str = str.split('.')[0]
  point_str = str.split('.')[1]

  return_value = ->() {
    return "#{int_str}.#{point_str}" if point>0
    int_str
  }

  if type=='text'
    return return_value.call if int_str.to_i<10000

    return "#{(int_str.to_i/10000.0).decimal_point(point)}" if int_str.to_i<100000000

    return "#{(int_str.to_i/100000000.0).decimal_point(point)}亿"
  end

  return return_value.call if int_str.length<3
  index = -4
  while index.abs < int_str.length+1
    int_str.insert(index, ',')
    index -=4
  end
  return_value.call
end

#to_size(point = 2) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/core_extend/float_module.rb', line 42

def to_size(point=2)
  str = decimal_point(point).to_s
  int_str = str.split('.')[0]
  point_str = str.split('.')[1]

  return_value = ->() {
    return "#{int_str}.#{point_str} bytes" if point>0
    int_str
  }

  return return_value.call if int_str.to_i<1000

  return "#{(int_str.to_i/1000.0).decimal_point(point)} KB" if int_str.to_i<1000*1000

  return "#{(int_str.to_i/(1000*1000.0)).decimal_point(point)} MB" if int_str.to_i<(1000*1000*1000)


  return return_value.call if int_str.length<3
  index = -4
  while index.abs < int_str.length+1
    int_str.insert(index, ',')
    index -=4
  end
  return_value.call
end