Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

C# Function to Calculate the Australian State from a Postcode

2.00/5 (3 votes)
15 Sep 2021CPOL2 min read 8.2K  
Lots of forms require the Postcode AND the State, when it is a fixed relationship. Use this function and remove the need to collect the State as well.
Many web forms and other systems ask for the postcode as well as the State. In Australia, it is a simple relationship and does not currently require a complex lookup table, since each State or Territory has a fixed numeric range of postcodes. By executing an IF-ELSE-ELSE-ELSE... one can easily determine the State. It also deals with the fact that most web forms will have data entered as strings, and attempts to parse it, or defaults to one of the States.

Introduction

In order to reduce the number of fields for our clients to fill out, we wanted a way to collect the most detailed information in the least number of form fields. In Australia, the Postcode is closely related to the State or Territory, and one could go for a full database lookup solution. However, a simpler solution given here is just to use the fact that Australian Postcodes are numeric, and that given numeric ranges are tied to specific States and Territories.

Background

The Australian Postal Service (called Australia Post) maintains the Postcodes register, and from their website and others, it is possible to look up the Postcode from an address, or a town name. However, there are times when the Postcode may be collected for one reason or another, such as to determine the closest store for a given business, and often the State is also requested. But we all know that more information required of our users just means less completions.

The Postcode in Australia is a 4 digit number, written as "0000" through to "9999", for example, Sydney has a Postcode of 2000.

Using the code

This is a simple function, written in C# but easily translatable to other languages.

The only parameter required is the string for the Postcode, which is likely to be what someone will have typed into a "Postcode" field on a form. It does not need to be a number that is entered.

The function returns a string being the conventional abbreviation used for one of the Australian States or Territories.

We are hard-coding to default to "NSW" if the string cannot be parsed as a number, or if the number does not fit within the ranges of Postcodes.

C#
static string stateFromPostcode(string postcodeString)
{
  string returnState = "NSW";
  int postcode = 0;

  // Get a numeric postcode from a string entered; if the TryParse fails, then
  // we will default to "NSW" anyway.
  bool result = Int32.TryParse(postcodeString, out postcode);
  if (result == true)
  {
    // These limits and assumptions are from https://postcodes-australia.com.
    // By making the postcode numeric, and then ordering the checking that
    // is done into numeric order, we can simplify the if-else-else to a simple
    // "if less than number" in the majority of States and Territories.
      
    if ((postcode > 799) && (postcode < 1000)) returnState = "NT";
    else if (postcode < 2000) returnState = "NSW";
    else if (((postcode > 2599) && (postcode < 2619)) ||
             ((postcode > 2899) && (postcode < 3000))) returnState = "ACT";
    else if (postcode < 3000) returnState = "NSW";
    else if (postcode < 4000) returnState = "VIC";
    else if (postcode < 5000) returnState = "QLD";
    else if (postcode < 6000) returnState = "SA";
    else if (postcode < 7000) returnState = "WA";
    else if (postcode < 8000) returnState = "TAS";
    else returnState = "NSW";
  } else returnState = "NSW";
  return returnState;
}

Points of Interest

See the Postcodes Australia website for a table stating the Postcode numeric ranges for Australian States and Territories.

History

  • 15th September, 2021: First version published (with a couple of typos!)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)