Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What Methond to use?

Status
Not open for further replies.

cdtech9198

IS-IT--Management
Feb 14, 2008
48
US
I am trying to output the suffix for streets. and avenue numbers But not sure what method to use to extract the first string. I need to use a switch statement for each of the cases below. What do you recommend I use for the following:

•Numbers with a 1 in the tens place (for example: 11, 14, 216) should have a 'th' suffix
•All other numbers ending in 1 should have a 'st' suffix
•All other numbers ending in 2 should have a 'nd' suffix
•All other numbers ending in 3 should have a 'rd' suffix
•All other Numbers should have a 'th' suffix

thank you.
 
I'd try and keep it fairly readable rather than try some convoluted piece of logic that looks funky:

Code:
		int units = num % 10;
		int tens = num % 100 - units;
		
		if(tens == 10) {
			System.out.println(num+"th");
		} else {
			switch(units) {
			case 1:
				System.out.println(num+"st");
				break;
			case 2:
				System.out.println(num+"nd");
				break;
			case 3:
				System.out.println(num+"rd");
				break;
			default:
				System.out.println(num+"th");
			}
		}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top