Class: Faker::Number

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

Constant Summary

Constants inherited from Base

Base::LLetters, Base::Letters, Base::NOT_GIVEN, 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(legacy_from = NOT_GIVEN, legacy_to = NOT_GIVEN, from: 1.00, to: 5000.00) ⇒ Integer

Produces a number between two provided values. Boundaries are inclusive.

Examples:

Faker::Number.between(from: 1, to: 10) #=> 7

Parameters:

  • from (Integer) (defaults to: 1.00)

    The lowest number to include.

  • to (Integer) (defaults to: 5000.00)

    The highest number to include.

Returns:

  • (Integer)

Available since:

  • 1.0.0


176
177
178
179
180
181
182
183
# File 'lib/faker/default/number.rb', line 176

def between(legacy_from = NOT_GIVEN, legacy_to = NOT_GIVEN, from: 1.00, to: 5000.00)
  warn_for_deprecated_arguments do |keywords|
    keywords << :from if legacy_from != NOT_GIVEN
    keywords << :to if legacy_to != NOT_GIVEN
  end

  Faker::Base.rand_in_range(from, to)
end

.decimal(legacy_l_digits = NOT_GIVEN, legacy_r_digits = NOT_GIVEN, l_digits: 5, r_digits: 2) ⇒ Float

Produces a float.

Examples:

Faker::Number.decimal(l_digits: 2) #=> 11.88
Faker::Number.decimal(l_digits: 3, r_digits: 3) #=> 181.843

Parameters:

  • l_digits (Integer) (defaults to: 5)

    Number of digits that the generated decimal should have to the left of the decimal point.

  • r_digits (Integer) (defaults to: 2)

    Number of digits that the generated decimal should have to the right of the decimal point.

Returns:

  • (Float)

Available since:

  • 1.0.0


81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/faker/default/number.rb', line 81

def decimal(legacy_l_digits = NOT_GIVEN, legacy_r_digits = NOT_GIVEN, l_digits: 5, r_digits: 2)
  warn_for_deprecated_arguments do |keywords|
    keywords << :l_digits if legacy_l_digits != NOT_GIVEN
    keywords << :r_digits if legacy_r_digits != NOT_GIVEN
  end

  l_d = number(digits: l_digits)

  # Ensure the last digit is not zero
  # so it does not get truncated on converting to float
  r_d = generate(r_digits - 1).join + non_zero_digit.to_s

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

.decimal_part(legacy_digits = NOT_GIVEN, digits: 10) ⇒ String

Produce a number with a number of digits, preserves leading zeroes.

Examples:

Faker::Number.decimal_part(digits: 2) #=> "09"

Parameters:

  • digits (Integer) (defaults to: 10)

    Number of digits that the generated number should have.

Returns:

  • (String)

Available since:

  • 1.0.0


56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/faker/default/number.rb', line 56

def decimal_part(legacy_digits = NOT_GIVEN, digits: 10)
  warn_for_deprecated_arguments do |keywords|
    keywords << :digits if legacy_digits != NOT_GIVEN
  end

  num = ''
  if digits > 1
    num = non_zero_digit
    digits -= 1
  end
  leading_zero_number(digits: digits) + num.to_s
end

.digitInteger

Produces a single-digit integer.

Examples:

Faker::Number.digit #=> 1

Returns:

  • (Integer)

Available since:

  • 1.0.0


118
119
120
# File 'lib/faker/default/number.rb', line 118

def digit
  rand(10)
end

.hexadecimal(legacy_digits = NOT_GIVEN, digits: 6) ⇒ String

Produces a number in hexadecimal format.

Examples:

Faker::Number.hexadecimal(digits: 3) #=> "e74"

Parameters:

  • digits (Integer) (defaults to: 6)

    Number of digits in the he

Returns:

  • (String)

Available since:

  • 1.0.0


132
133
134
135
136
137
138
139
140
# File 'lib/faker/default/number.rb', line 132

def hexadecimal(legacy_digits = NOT_GIVEN, digits: 6)
  warn_for_deprecated_arguments do |keywords|
    keywords << :digits if legacy_digits != NOT_GIVEN
  end

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

.leading_zero_number(legacy_digits = NOT_GIVEN, digits: 10) ⇒ String

Produce a random number with a leading zero.

Examples:

Faker::Number.leading_zero_number(digits: 10) #=> "0669336915"

Parameters:

  • digits (Integer) (defaults to: 10)

    Number of digits that the generated number should have.

Returns:

  • (String)

Available since:

  • 1.0.0


38
39
40
41
42
43
44
# File 'lib/faker/default/number.rb', line 38

def leading_zero_number(legacy_digits = NOT_GIVEN, digits: 10)
  warn_for_deprecated_arguments do |keywords|
    keywords << :digits if legacy_digits != NOT_GIVEN
  end

  '0' + (2..digits).collect { digit }.join
end

.negative(legacy_from = NOT_GIVEN, legacy_to = NOT_GIVEN, from: -5000.00,, to: -1.00)) ⇒ Float

Produces a negative float.

Examples:

Faker::Number.negative #=> -4480.042585669558

Parameters:

  • from (Integer) (defaults to: -5000.00,)

    The lower boundary.

  • to (Integer) (defaults to: -1.00))

    The higher boundary.

Returns:

  • (Float)

Available since:

  • 1.0.0


236
237
238
239
240
241
242
243
244
245
# File 'lib/faker/default/number.rb', line 236

def negative(legacy_from = NOT_GIVEN, legacy_to = NOT_GIVEN, from: -5000.00, to: -1.00)
  warn_for_deprecated_arguments do |keywords|
    keywords << :from if legacy_from != NOT_GIVEN
    keywords << :to if legacy_to != NOT_GIVEN
  end

  random_number = between(from: from, to: to)

  less_than_zero(random_number)
end

.non_zero_digitInteger

Produces a non-zero single-digit integer.

Examples:

Faker::Number.non_zero_digit #=> 8

Returns:

  • (Integer)

Available since:

  • 1.0.0


105
106
107
# File 'lib/faker/default/number.rb', line 105

def non_zero_digit
  rand(1..9)
end

.normal(legacy_mean = NOT_GIVEN, legacy_standard_deviation = NOT_GIVEN, mean: 1, standard_deviation: 1) ⇒ Float

Produces a float given a mean and standard deviation.

Examples:

Faker::Number.normal(mean: 50, standard_deviation: 3.5) #=> 47.14669604069156

Parameters:

  • mean (Integer) (defaults to: 1)
  • standard_deviation (Integer, Float) (defaults to: 1)

Returns:

  • (Float)

Available since:

  • 1.0.0


153
154
155
156
157
158
159
160
161
162
163
# File 'lib/faker/default/number.rb', line 153

def normal(legacy_mean = NOT_GIVEN, legacy_standard_deviation = NOT_GIVEN, mean: 1, standard_deviation: 1)
  warn_for_deprecated_arguments do |keywords|
    keywords << :mean if legacy_mean != NOT_GIVEN
    keywords << :standard_deviation if legacy_standard_deviation != NOT_GIVEN
  end

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

.number(legacy_digits = NOT_GIVEN, digits: 10) ⇒ Integer

Produce a random number.

Examples:

Faker::Number.number(digits: 10) #=> 1968353479

Parameters:

  • digits (Integer) (defaults to: 10)

    Number of digits that the generated number should have.

Returns:

  • (Integer)

Available since:

  • 1.0.0


16
17
18
19
20
21
22
23
24
25
26
# File 'lib/faker/default/number.rb', line 16

def number(legacy_digits = NOT_GIVEN, digits: 10)
  warn_for_deprecated_arguments do |keywords|
    keywords << :digits if legacy_digits != NOT_GIVEN
  end

  return if digits < 1
  return rand(0..9).round if digits == 1

  # Ensure the first digit is not zero
  ([non_zero_digit] + generate(digits - 1)).join.to_i
end

.positive(legacy_from = NOT_GIVEN, legacy_to = NOT_GIVEN, from: 1.00, to: 5000.00) ⇒ Float

Produces a positive float.

Examples:

Faker::Number.positive #=> 235.59238499107653

Parameters:

  • from (Integer) (defaults to: 1.00)

    The lower boundary.

  • to (Integer) (defaults to: 5000.00)

    The higher boundary.

Returns:

  • (Float)

Available since:

  • 1.0.0


214
215
216
217
218
219
220
221
222
223
# File 'lib/faker/default/number.rb', line 214

def positive(legacy_from = NOT_GIVEN, legacy_to = NOT_GIVEN, from: 1.00, to: 5000.00)
  warn_for_deprecated_arguments do |keywords|
    keywords << :from if legacy_from != NOT_GIVEN
    keywords << :to if legacy_to != NOT_GIVEN
  end

  random_number = between(from: from, to: to)

  greater_than_zero(random_number)
end

.within(legacy_range = NOT_GIVEN, range: 1.00..5000.00) ⇒ Integer

Produces a number within two provided values. Boundaries are inclusive or exclusive depending on the range passed.

Examples:

Faker::Number.within(range: 1..10) #=> 7

Parameters:

  • range (Range) (defaults to: 1.00..5000.00)

    The range from which to generate a number.

Returns:

  • (Integer)

Available since:

  • 1.0.0


195
196
197
198
199
200
201
# File 'lib/faker/default/number.rb', line 195

def within(legacy_range = NOT_GIVEN, range: 1.00..5000.00)
  warn_for_deprecated_arguments do |keywords|
    keywords << :range if legacy_range != NOT_GIVEN
  end

  between(from: range.min, to: range.max)
end