|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
returning an appropriate string for an expected int value??
Hi can anyone help?
Im trying to write a single function which sets a small array with 2 characters to the ordinal suffix of a number. 1st, 2nd, 3rd 4th etc and the suffix refers to the laters. my function prototype is void suffix(int n, char suff[]); and i have made the following declarations char s[5]; int x; and with the result of the call suffix(x,s); will give the appropriate string in s for any given positive value of x from 0 to 9999 a possible pattern i have found is that 4 - 20 end in th then the first three after that have st,nd,rd is there a command that can read the last digit of an integer number ? because if there is then i can use conditions easier and compare the last digit against the tests to see what its suffix ( letters ) are. any help much appreciated. |
|
#2
|
||||
|
||||
|
Find the result of the number modulo 10. That will give you the last digit.
last_digit = number % 10; |
|
#3
|
|||
|
|||
|
im not sure if that will work
if the user types 5006 the suffix will be 5006 th because the last digit is 6 which has a suffix of th i will get a decimal number when im storing in an integer. |
|
#4
|
||||
|
||||
|
>> i will get a decimal number when im storing in an integer.
I'm not sure what you mean by this. Do you mean a floating point number?? You're not going to get floating point numbers when your variables are declared as int. Code:
void suffix(int n, char suff[]) {
int last_digit = n % 10;
if (last_digit == 1)
sprintf(suff, "%dst", n);
else if (last_digit == 2)
sprintf(suff, "%dnd", n);
else if (last_digit == 3)
sprintf(suff, "%drd", n);
else
sprintf(suff, "%dth", n);
}
Hope this helps! ![]() |
|
#5
|
|||
|
|||
|
Quote:
thanks for the reply could you do me a favour and explain why you are using modulus 10 and what the sprintf command is doing. I havent come across it before thats why. Will your method work if the user types in 6777 as the number will it be able to recongnize the last digit which is 7 and assign it a th ? |
|
#6
|
||||
|
||||
|
>> could you do me a favour and explain why you are using modulus 10
It's modulo, not modulus. What the modulo operator does is to return the remainder of a division operation. For example, 5437 modulo 10 returns 7 (i.e. 5437 / 10 = 543 with a remainder of 7). Hence, when you modulo divide a number by 10, you get the last digit. >> what the sprintf command is doing sprintf works similar to printf, except it puts the result into the first argument, instead of the screen. I'm assuming here that you know what the printf() function does. So in our case sprintf(suff, "%dst", n) will put the result into the suff char array. >> Will your method work if the user types in 6777 Yes it will. |
|
#7
|
||||
|
||||
|
Scorpions4ever, remember what Neildadon stated in the first post: the numbers 11, 12 and 13 do not end with 'st', 'nd' or 'rd' - they end with 'th'. Your code does not check these conditions. The last 2 digits of the number must be checked for these 3 cases (x % 100 would suffice to get the last 2 digits). Then, your code would be fine (x %10 to get the last digit).
__________________
Jason Doucette / Xona.com™ - Programming Windows Errata Addendum "Discussion is an exchange of knowledge; argument is an exchange of ignorance." |
|
#8
|
||||
|
||||
|
Good call. I was only paying attention to this part of his post:
>> is there a command that can read the last digit of an integer number ? That's the part I was paying attention to ![]() |
|
#9
|
||||
|
||||
|
Understood.
I just thought I'd mention it in case a passerby grabbed your code unknowingly thinking it would handle the original question asked. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > returning an appropriate string for an expected int value?? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|