Skip to content

Instantly share code, notes, and snippets.

@fsarradin
Created December 11, 2011 23:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fsarradin/1463361 to your computer and use it in GitHub Desktop.
Save fsarradin/1463361 to your computer and use it in GitHub Desktop.
Check if a given code is a consistent with the ISIN standard
package isin
object Isin {
// based on http://en.wikipedia.org/wiki/International_Securities_Identification_Number
def isIsin(code: String): Boolean = {
val digits: String = code.init.map(c => BigInt(c.toString, 36)).mkString
val parity = digits.size & 1
val total: Int = digits.zipWithIndex.map {
case (c: Char, i: Int) =>
val digit: Int = c.asDigit
val result: Int = if ((i & 1) != parity) digit << 1 else digit
result / 10 + result % 10
}.sum
val tenComplement: Int = (10 - (total % 10)) % 10
tenComplement == code.last.asDigit
}
def main(args: Array[String]) {
val isinSg: String = "FR0000130809"
println(isIsin(isinSg))
val isinBad: String = "FR1234567898"
println(isIsin(isinBad))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment