Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#
Tip/Trick

C# Function to Calculate the Australian State from a Postcode

Rate me:
Please Sign up or sign in to vote.
2.00/5 (3 votes)
15 Sep 2021CPOL2 min read 6.9K   1   13
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)


Written By
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSeriously Pin
Member 1536973124-Sep-21 2:53
Member 1536973124-Sep-21 2:53 
AnswerRe: Seriously Pin
Philip Ryan24-Sep-21 21:00
Philip Ryan24-Sep-21 21:00 
SuggestionYour code can be much simpler Pin
Dmitrii Evdokimov17-Sep-21 4:31
Dmitrii Evdokimov17-Sep-21 4:31 
GeneralRe: Your code can be much simpler Pin
Philip Ryan17-Sep-21 13:35
Philip Ryan17-Sep-21 13:35 
GeneralRe: Your code can be much simpler Pin
Adam David Hill20-Sep-21 7:02
professionalAdam David Hill20-Sep-21 7:02 
PraiseRe: Your code can be much simpler Pin
Dmitrii Evdokimov23-Sep-21 22:45
Dmitrii Evdokimov23-Sep-21 22:45 
QuestionIt's on github Pin
Michael Hanlon 202116-Sep-21 13:03
Michael Hanlon 202116-Sep-21 13:03 
AnswerRe: It's on github Pin
Philip Ryan17-Sep-21 0:55
Philip Ryan17-Sep-21 0:55 
Didn’t see it before your comment, so thanks for that.

That said, that solution (at GitHub - matthewproctor/australianpostcodes: A community sourced comprehensive database of Australian Post Codes with geolocation data.[^]) is the full database solution that I was trying to avoid and go for a simple function.

Cool, though! Wink | ;)
QuestionNOTE: 2620 is used in both NSW and ACT Pin
Member 1377192216-Sep-21 10:20
Member 1377192216-Sep-21 10:20 
AnswerRe: NOTE: 2620 is used in both NSW and ACT Pin
Philip Ryan16-Sep-21 23:14
Philip Ryan16-Sep-21 23:14 
QuestionHo ho ho, Santa Pin
Ben Code16-Sep-21 0:58
Ben Code16-Sep-21 0:58 
AnswerRe: Ho ho ho, Santa Pin
Philip Ryan17-Sep-21 0:55
Philip Ryan17-Sep-21 0:55 
QuestionNeeds a few more cases Pin
Member 1255812215-Sep-21 22:39
Member 1255812215-Sep-21 22:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.