AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   [ANY] Singular and Plural (https://forums.alliedmods.net/showthread.php?t=294076)

Totenfluch 02-19-2017 09:15

[ANY] Singular and Plural
 
I've seen lots of if statements because of this very problem. Here is an easy solution:


PHP Code:

int coins 10;
PrintToChat(client"You have %i Coin%s"coinscoins==1?"":"s"); 


Addicted. 02-19-2017 14:14

Re: [ANY] Singular and Plural
 
For anyone interested in learning more on how to use this in other situations it is called a ternary operator:

https://en.wikipedia.org/wiki/%3F:#Usage

404UserNotFound 02-19-2017 15:02

Re: [ANY] Singular and Plural
 
I love ternary operators. My recent discovery of how they work (the ternary operator being the ? symbol) has given me so many ways compressing small if statements like the one Totenfluch provided.

:D

Chdata 02-19-2017 15:04

Re: [ANY] Singular and Plural
 
Code:

//  Evaluates to a string of the ordinal suffix of a number (1st, 2nd, 3rd, etc)
#define NatSuf(%1)              ((%1 + 9) % 10 >= 3 || (%1 + 89) % 100 < 3 ? "th" : (%1 % 10) == 1 ? "st" : (%1 % 10) == 2 ? "nd" : "rd")


404UserNotFound 02-19-2017 17:38

Re: [ANY] Singular and Plural
 
Quote:

Originally Posted by Chdata (Post 2496699)
Code:

//  Evaluates to a string of the ordinal suffix of a number (1st, 2nd, 3rd, etc)
#define NatSuf(%1)              ((%1 + 9) % 10 >= 3 || (%1 + 89) % 100 < 3 ? "th" : (%1 % 10) == 1 ? "st" : (%1 % 10) == 2 ? "nd" : "rd")


Woah. Are you a wizard?

Also, I challenge you to break the all down part by part and explain what each thing means and does :P

KyleS 02-19-2017 19:52

Re: [ANY] Singular and Plural
 
Quote:

Originally Posted by Chdata (Post 2496699)
Code:

//  Evaluates to a string of the ordinal suffix of a number (1st, 2nd, 3rd, etc)
#define NatSuf(%1)              ((%1 + 9) % 10 >= 3 || (%1 + 89) % 100 < 3 ? "th" : (%1 % 10) == 1 ? "st" : (%1 % 10) == 2 ? "nd" : "rd")


Ah yes, the 0th :wink:

Instead of going into macro, modulo, and ternary hell, try something untested like this:
PHP Code:

static stock NatStuf(number)
{
    static const 
String:InternalPrefix[][] = {"""st""nd""rd""th"};
    new 
mod = (number 10);
    if (
mod 3)
        
mod = (sizeof(InternalPrefix) - 1);
    
    return 
_:InternalPrefix[mod];



Neuro Toxin 02-20-2017 18:06

Re: [ANY] Singular and Plural
 
Ternary operators are usefull but can make code difficult to read.

friagram 02-20-2017 20:13

Re: [ANY] Singular and Plural
 
You can make a translations phrase for single and plural.
Not all languages express plural the same

psychonic 02-20-2017 20:22

Re: [ANY] Singular and Plural
 
Quote:

Originally Posted by friagram (Post 2497034)
You can make a translations phrase for single and plural.
Not all languages express plural the same

Nor are all languages consistent with how words are pluralized, including English. Ex. "s" versus "es", not to mention English words that have origins in other languages.

Chdata 02-25-2017 13:34

Re: [ANY] Singular and Plural
 
It was funny when I made a bool for gender

And then realized that army tanks are an "it".


All times are GMT -4. The time now is 03:53.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.