Easy Pluralization-VBForums
Results 1 to 5 of 5

Thread: Easy Pluralization

  1. #1

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Talking Easy Pluralization

    It's always annoying to spend time working with ternary if statements and what not, or even (gasp) using a variable to pluralize things. Here's a snippet that makes it easier.

    http://jsfiddle.net/minitech/PefYd/

    You use it like so:
    Code:
    var str = "There {is|are} # apples. The #% apple looks nice to me.".plural(5);
    Which gives:

    Code:
    "There are 5 apples. The 5th apple looks nice to me."
    # is replaced with the passed integer. % becomes st, nd, rd, or th. {x} becomes x if the number is plural, nothing otherwise. {x|y} becomes x if the number is singular, y if it is plural. More than 2 results in 1 being turned into the first one, 2 to the second one, 3 to the third one, etc. and all out of range values being turned into the last one.

    Enjoy! Suggestions are welcome.
    Last edited by minitech; Jun 20th, 2011 at 07:35 PM.

  2. #2
    PowerPoster
    Join Date
    May 2002
    Posts
    25,961

    Re: Easy Pluralization

    Haven't looked at the code... but might need some more work as "There are 1 apples. The 1st apple looks nice to me." doesn't work either... specifically "There are 1 apples." ??? Even "There is 1 apples." doesn't work. I'm just saying. :P

    Would it need to be done like this?
    var str = "There {is|are} # {apple|apples}. The #% apple looks nice to me.".plural(1);
    Would it work at that point?
    I will admit... that's pretty cool...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Easy Pluralization

    Oops, it's supposed to be apple{s} instead. Sorry.

  4. #4
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: Easy Pluralization

    In what kind of program would I use this?
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  5. #5

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Easy Pluralization

    In any sort of page where you need pluralization. For example, on websites (like SourceForge) that say "Your download will start in 1 seconds". Using this, the "seconds" can turn into "second" at the right time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.