Skip to content

Instantly share code, notes, and snippets.

@syedadeel2
Last active July 19, 2022 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syedadeel2/4a308a974fa1b4122a423a5bb285d584 to your computer and use it in GitHub Desktop.
Save syedadeel2/4a308a974fa1b4122a423a5bb285d584 to your computer and use it in GitHub Desktop.
C# - ACN & ABN Digit Check
// Reference
// https://abr.business.gov.au/Help/AbnFormat
public static bool IsValidABN(string abn)
{
// return false if null
if ( abn == null ) return false;
// remove spaces
abn = abn.Replace( " ", "" );
// return false if length is not 11
if ( abn.Length != 11 ) return false;
// return false if not all digits
if ( !abn.All( char.IsDigit ) ) return false;
int[] weights = {10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
// 1. Subtract 1 from the first (left) digit to give a new eleven digit number
// 2. Multiply each of the digits in this new number by its weighting factor
// 3. Sum the resulting 11 products
// 4. Divide the sum by 89 to get the check digit.
// 5. If the remainder is zero the number is valid
return abn.Where( x => char.IsDigit( x ) )
.Select( (x, n) => ( x - '0' ) - ( n == 0 ? 1 : 0 ) )
.Zip( weights, (d, w) => d * w )
.Sum() % 89 == 0;
}
// Reference
// https://asic.gov.au/for-business/registering-a-company/steps-to-register-a-company/australian-company-numbers/australian-company-number-digit-check/
public static bool IsValidACN(string acn)
{
// return false if null
if ( acn == null ) return false;
// remove spaces
acn = acn.Replace( " ", "" );
// return false if length is not 9
if ( acn.Length != 9 ) return false;
// return false if not all digits
if ( !acn.All( char.IsDigit ) ) return false;
// Step 1 - Apply weighting to digits 1 to 8.
int[] weights = { 8, 7, 6, 5, 4, 3, 2, 1 };
// Step 2 - Sum the products
// Step 3 - Divide by 10 to obtain remainder
var remainder = acn.Where( x => char.IsDigit( x ) )
.Select( (x, n) => (x - '0') * (weights.Length == n ? 0 : weights[n]))
.Sum() % 10;
// Step 4 - Complement the remainder to 10
var complement = 10 - remainder;
// If the complement equals 10, set it to 0.
complement = complement == 10 ? 0 : complement;
// Step 5 - Check if the last digit is equal to the complement.
return ( acn.Last() - '0' ) == complement;
}
public static (bool Valid, string TypeName) IsABNOrACN(string numbers)
{
numbers = numbers.Replace( " ", "" );
bool isValid = numbers.Length == 11 || numbers.Length == 9;
string typeName = "Unknown";
if ( numbers.Length == 9 ) typeName = "ACN";
else if ( numbers.Length == 11 ) typeName = "ABN";
return (isValid, typeName);
}
public static void RunTest()
{
var testData = new string[]{ "", "asasda", "12313", "51 824 753 556", "12 004 045 440", "000 500 005", "000 000 019","000 250 000","000 500 005","000 750 005","001 000 004","001 250 004","001 500 009","001 729 999","001 999 999","002 249 998","002 499 998","002 749 993","002 999 993","003 249 992","003 499 992","003 749 988","003 999 988","004 249 987","004 499 987","004 749 982","004 999 982","005 249 981","005 499 981","005 749 986","005 999 977","006 249 976","006 499 976","006 749 980","006 999 980","007 249 989","007 499 989","007 749 975","007 999 975","008 249 974","008 499 974","008 749 979","008 999 979","009 249 969","009 499 969","009 749 964","009 999 964","010 249 966","010 499 966","010 749 961" };
foreach ( var data in testData )
{
var (Valid, TypeName) = IsABNOrACN( data );
if ( Valid )
{
var isValid = TypeName == "ABN" ? IsValidABN( data ) : IsValidACN( data );
Console.WriteLine( string.Format( "{0} is a valid {1}? = {2}", data, TypeName, isValid ? "Yes" : "No" ) );
}
else
{
Console.WriteLine( string.Format( "{0} is not a valid ABN or ACN", data ) );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment