How to compute a check digit for a large number in R?

user3341592

I must add a 2-digit check (remaining of division by 97) to numbers such as 52/200005/0001 (slashes must be ignored).

My code is the following, but it fails because of too big number:

AppendCheckDigits <- function (x) {
  stopifnot(is.character(x) & length(x) == 1 & nchar(x) == 14)
  cd <- as.integer(paste(substring(x, 1, 2),
                         substring(x, 4, 9),
                         substring(x, 11, 14), sep="")) %% 97
  paste(x, "/", cd, sep="")
}

Test it with:

AppendCheckDigits("52/200005/0001")

How can I solve it?

juba

Objects of class integer are restricted to about 2*10^9. You should use as.numeric instead of as.integer in your function :

AppendCheckDigits <- function (x) {
  stopifnot(is.character(x) & length(x) == 1 & nchar(x) == 14)
  cd <- as.numeric(paste(substring(x, 1, 2),
                         substring(x, 4, 9),
                         substring(x, 11, 14), sep="")) %% 97
  paste(x, "/", cd, sep="")
}

Then :

> AppendCheckDigits("52/200005/0001")
[1] "52/200005/0001/43"

Note that you could vectorize and simplify your function this way, for example :

AppendCheckDigits <- function (x) {
  stopifnot(is.character(x) & nchar(x) == rep(14,length(x)))
  num <- as.numeric(gsub("/","", x)) %% 97
  return(paste0(x, "/", num))
}

Then :

> AppendCheckDigits(c("52/200005/0001", "52/200005/0021"))
[1] "52/200005/0001/43" "52/200005/0021/63"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to compute a check digit for a large number in R?

From Dev

How to check if a three digit number is a palindrome?

From Dev

How to find a specific digit from large number and count that?

From Java

Java - How to check if a 13 digit isbn number is valid

From Dev

Scala : How to check every single digit of a number is even or odd?

From Dev

If Statement to check last digit of number

From Dev

Check exact digit in number in SQL

From Dev

How do I get the last digit (15th luhn check digit) for a given 14 digit number with sql server

From Dev

Check if number is too large or not a number

From Dev

How to make R display 19 digit number as it is stored?

From Dev

in R how to compute means of increasing number/order of sequence

From Dev

Check for two decimal digit number in string

From Dev

Python 3 - Check if digit inside a number with if comprehension

From Dev

How to compute the remainder of a very large number (string with 1 mi digits) in the division by 1500

From Dev

How to get Last digit of number

From Dev

How to get the fist digit of a number

From Dev

How to convert bit number to digit

From Java

Fastest way in R to compute the inverse for large matrices

From Dev

How to check CUDA Compute Capability?

From Dev

How to check if a big number is a square of an integer in R?

From Dev

Print a single large number with underlined 3-digit groups as in a tibble?

From Dev

How to compute summation in r

From Dev

Generating a 20-digit number in R

From Dev

Frequency of most repeated digit in a number in R

From Dev

How to check how many line start with digit

From Dev

How to check how many line start with digit

From Dev

What's the efficient algorithm to find the Integer square root of a very large number, digit by digit?

From Dev

What's the efficient algorithm to find the Integer square root of a very large number, digit by digit?

From Dev

How do i find the highest digit in a multi-digit number?

Related Related

  1. 1

    How to compute a check digit for a large number in R?

  2. 2

    How to check if a three digit number is a palindrome?

  3. 3

    How to find a specific digit from large number and count that?

  4. 4

    Java - How to check if a 13 digit isbn number is valid

  5. 5

    Scala : How to check every single digit of a number is even or odd?

  6. 6

    If Statement to check last digit of number

  7. 7

    Check exact digit in number in SQL

  8. 8

    How do I get the last digit (15th luhn check digit) for a given 14 digit number with sql server

  9. 9

    Check if number is too large or not a number

  10. 10

    How to make R display 19 digit number as it is stored?

  11. 11

    in R how to compute means of increasing number/order of sequence

  12. 12

    Check for two decimal digit number in string

  13. 13

    Python 3 - Check if digit inside a number with if comprehension

  14. 14

    How to compute the remainder of a very large number (string with 1 mi digits) in the division by 1500

  15. 15

    How to get Last digit of number

  16. 16

    How to get the fist digit of a number

  17. 17

    How to convert bit number to digit

  18. 18

    Fastest way in R to compute the inverse for large matrices

  19. 19

    How to check CUDA Compute Capability?

  20. 20

    How to check if a big number is a square of an integer in R?

  21. 21

    Print a single large number with underlined 3-digit groups as in a tibble?

  22. 22

    How to compute summation in r

  23. 23

    Generating a 20-digit number in R

  24. 24

    Frequency of most repeated digit in a number in R

  25. 25

    How to check how many line start with digit

  26. 26

    How to check how many line start with digit

  27. 27

    What's the efficient algorithm to find the Integer square root of a very large number, digit by digit?

  28. 28

    What's the efficient algorithm to find the Integer square root of a very large number, digit by digit?

  29. 29

    How do i find the highest digit in a multi-digit number?

HotTag

Archive