Method: Faker::NationalHealthService.check_digit

Defined in:
lib/faker/default/national_health_service.rb

.check_digit(number: 0) ⇒ Integer (private)

Produces a random British NHS number’s check digit.

Examples:

Faker::NationalHealthService.check_digit(number: 400_012_114) #=> 6

Parameters:

  • number (Integer) (defaults to: 0)

    Specifies the NHS number the check digit belongs to.

Returns:

  • (Integer)

Available since:

  • 1.9.2



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/faker/default/national_health_service.rb', line 41

def check_digit(number: 0)
  sum = 0
  number.to_s.chars.each_with_index do |digit, idx|
    position = idx + 1
    sum += (digit.to_i * (11 - position))
  end
  result = 11 - (sum % 11)

  # A value of 11 is considered the same as 0
  # See https://en.wikipedia.org/wiki/NHS_number
  return 0 if result == 11

  result
end