Class: Faker::Number

Inherits:
Base
  • Object
show all
Extended by:
Gem::Deprecate
Defined in:
lib/faker/default/number.rb

Constant Summary

Constants inherited from Base

Base::Letters, Base::Numbers, Base::ULetters

Class Method Summary collapse

Methods inherited from Base

bothify, disable_enforce_available_locales, fetch, fetch_all, flexible, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, translate, unique, with_locale

Class Method Details

.between(from = 1.00, to = 5000.00) ⇒ Object


60
61
62
# File 'lib/faker/default/number.rb', line 60

def between(from = 1.00, to = 5000.00)
  Faker::Base.rand_in_range(from, to)
end

.decimal(l_digits = 5, r_digits = 2) ⇒ Object


32
33
34
35
36
37
# File 'lib/faker/default/number.rb', line 32

def decimal(l_digits = 5, r_digits = 2)
  l_d = number(l_digits)
  r_d = decimal_part(r_digits)

  "#{l_d}.#{r_d}"
end

.decimal_part(digits = 10) ⇒ Object


23
24
25
26
27
28
29
30
# File 'lib/faker/default/number.rb', line 23

def decimal_part(digits = 10)
  num = ''
  if digits > 1
    num = non_zero_digit
    digits -= 1
  end
  leading_zero_number(digits) + num
end

.digitObject


43
44
45
# File 'lib/faker/default/number.rb', line 43

def digit
  rand(10).to_s
end

.hexadecimal(digits = 6) ⇒ Object


47
48
49
50
51
# File 'lib/faker/default/number.rb', line 47

def hexadecimal(digits = 6)
  hex = ''
  digits.times { hex += rand(15).to_s(16) }
  hex
end

.leading_zero_number(digits = 10) ⇒ Object


19
20
21
# File 'lib/faker/default/number.rb', line 19

def leading_zero_number(digits = 10)
  '0' + (2..digits).collect { digit }.join
end

.negative(from = -5000.00,, to = -1.00)) ⇒ Object


74
75
76
77
78
# File 'lib/faker/default/number.rb', line 74

def negative(from = -5000.00, to = -1.00)
  random_number = between(from, to)

  less_than_zero(random_number)
end

.non_zero_digitObject


39
40
41
# File 'lib/faker/default/number.rb', line 39

def non_zero_digit
  rand(1..9).to_s
end

.normal(mean = 1, standard_deviation = 1) ⇒ Object


53
54
55
56
57
58
# File 'lib/faker/default/number.rb', line 53

def normal(mean = 1, standard_deviation = 1)
  theta = 2 * Math::PI * rand
  rho = Math.sqrt(-2 * Math.log(1 - rand))
  scale = standard_deviation * rho
  mean + scale * Math.cos(theta)
end

.number(digits = 10) ⇒ Object


8
9
10
11
12
13
14
15
16
17
# File 'lib/faker/default/number.rb', line 8

def number(digits = 10)
  num = ''

  if digits > 1
    num = non_zero_digit
    digits -= 1
  end

  num + (1..digits).collect { digit }.join
end

.positive(from = 1.00, to = 5000.00) ⇒ Object


68
69
70
71
72
# File 'lib/faker/default/number.rb', line 68

def positive(from = 1.00, to = 5000.00)
  random_number = between(from, to)

  greater_than_zero(random_number)
end

.within(range = 1.00..5000.00) ⇒ Object


64
65
66
# File 'lib/faker/default/number.rb', line 64

def within(range = 1.00..5000.00)
  between(range.min, range.max)
end