Click here to Skip to main content
15,124,055 members
Articles / Programming Languages / C#
Tip/Trick
Posted 15 Sep 2021

Tagged as

Stats

2.8K views
1 bookmarked

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
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)

Share

About the Author

Philip Ryan
Australia Australia
No Biography provided

Comments and Discussions

 
QuestionSeriously Pin
Member 1536973124-Sep-21 3:53
MemberMember 1536973124-Sep-21 3:53 
AnswerRe: Seriously Pin
Philip Ryan24-Sep-21 22:00
MemberPhilip Ryan24-Sep-21 22:00 
SuggestionYour code can be much simpler Pin
Dmitrii Evdokimov17-Sep-21 5:31
MemberDmitrii Evdokimov17-Sep-21 5:31 
GeneralRe: Your code can be much simpler Pin
Philip Ryan17-Sep-21 14:35
MemberPhilip Ryan17-Sep-21 14:35 
GeneralRe: Your code can be much simpler Pin
Adam David Hill20-Sep-21 8:02
professionalAdam David Hill20-Sep-21 8:02 
Building on Dmitrii's answer, if you are using C# 9 or greater you can represent this very cleanly with pattern-matching syntax:

C#
static string StateFromPostcode(string postcodeString)
    => int.TryParse(postcodeString, out int postcode)
    ? PostCodeState(postcode)
    : "NWS";

static string PostCodeState(int postcode) 
    => postcode switch
    {
        < 800 => "NSW",
        < 1000 => "NT",
        < 2600 => "NSW",
        < 2619 => "ACT",
        < 2900 => "NSW",
        < 2921 => "ACT",
        < 3000 => "NSW",
        < 4000 => "VIC",
        < 5000 => "QLD",
        < 6000 => "SA",
        < 7000 => "WA",
        < 8000 => "TAS",
        9999 => "North Pole, VIC 9999",
        _ => "NSW"
    };

PraiseRe: Your code can be much simpler Pin
Dmitrii Evdokimov23-Sep-21 23:45
MemberDmitrii Evdokimov23-Sep-21 23:45 
QuestionIt's on github Pin
Michael Hanlon 202116-Sep-21 14:03
MemberMichael Hanlon 202116-Sep-21 14:03 
AnswerRe: It's on github Pin
Philip Ryan17-Sep-21 1:55
MemberPhilip Ryan17-Sep-21 1:55 
QuestionNOTE: 2620 is used in both NSW and ACT Pin
Member 1377192216-Sep-21 11:20
MemberMember 1377192216-Sep-21 11:20 
AnswerRe: NOTE: 2620 is used in both NSW and ACT Pin
Philip Ryan17-Sep-21 0:14
MemberPhilip Ryan17-Sep-21 0:14 
QuestionHo ho ho, Santa Pin
Ben Code16-Sep-21 1:58
MemberBen Code16-Sep-21 1:58 
AnswerRe: Ho ho ho, Santa Pin
Philip Ryan17-Sep-21 1:55
MemberPhilip Ryan17-Sep-21 1:55 
QuestionNeeds a few more cases Pin
Member 1255812215-Sep-21 23:39
MemberMember 1255812215-Sep-21 23: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.