Skip to content

Instantly share code, notes, and snippets.

@susannej
Created April 6, 2016 11:47
Show Gist options
  • Save susannej/223c89c4b6c5ec1b7b961aa0a098abd6 to your computer and use it in GitHub Desktop.
Save susannej/223c89c4b6c5ec1b7b961aa0a098abd6 to your computer and use it in GitHub Desktop.
IBAN (International Bank Account Number) mini tools (check, trim, expand)
// really, really, really simple IBAN check and helper methods
boolean ibanCheck(String iban) {
String iban2 = iban2DB(iban)
String bban = iban2.substring(4)
String lkzPrf = iban2.substring(0,4)
String ibanDec = ""
(bban + lkzPrf).each { c ->
if (c >= "0" && c <= "9")
ibanDec += c
else if (c >= "A" && c <= "Z")
ibanDec += ((c as char) - ('A' as char) + 10).toString()
}
(new BigInteger(ibanDec)) % 97 == 1 ? true : false
}
// transform iban for db and/or sepa-xml etc.
def iban2DB(String iban) {
iban.replaceAll("\\s", "").toUpperCase()
}
// transform eban for displaying and printing
def iban2Display(String iban) {
String iban2 = iban2DB(iban)
def ibanDisp = ""
for (int i = 0; i < iban2.length(); i += 4) {
if (i > 0)
ibanDisp += " "
ibanDisp += iban2.substring(i, Math.min(i +4, iban2.length()))
}
ibanDisp
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment