C# Function to Calculate the Australian State from a Postcode





5.00/5 (5 votes)
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.
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 Postcode
s.
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!)