Created
December 11, 2011 23:06
-
-
Save fsarradin/1463361 to your computer and use it in GitHub Desktop.
Check if a given code is a consistent with the ISIN standard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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