# return up to 5 numbers separated with commas

## Recommended Posts

I am allowing the user to input UP TO 5 sequential numbers.

(example): 3, 4, 5, 6, 7

they can have up to 5, but they can have anywhere from 1 to 5.

rule needs to do 2 things:

1. depending on how many numbers they enter, all numbers should be separated by a comma (and a space) ... and the LAST number they enter should come after the word 'and'

2. convert the numbers into ordinals. example: "1" becomes "1st" ... "3" becomes "3rd" ... "8" becomes "8th"

example 1:

number1 = "3"

number2 = "4"

number3 = "5"

number4 = ""

number5 = ""

return "3rd, 4th and 5th"

example 2:

number1 = "5"

number2 = "6"

number3 = "7"

number4 = "8"

number5 = "9"

return "5th, 6th, 7th, 8th and 9th"

##### Share on other sites

Eric,

This is one solution. You could probably do it much more efficiently through the use of an array, but this is what I came up with in a simple layout form. This would be your entire rule that you would call for the output string that you want.

```function ordinal(num)  {
var mod1 = num%100;
var mod2 = num%10;
var ord = "";
if((mod1-mod2) == 10)  {
ord = "th";
}
else  {
switch(mod2)  {
case 1: ord = "st";
break;
case 2: ord = "nd";
break;
case 3: ord = "rd";
break;
default: ord = "th";
break;
}
}
return num+ord;
}

var ordString = "";
if (Trim(Field("Num1")) != "")  {
ordString += ordinal(Field("Num1"));
}
if (Trim(Field("Num2")) != "")  {
ordString += ", " + ordinal(Field("Num2"));
}
if (Trim(Field("Num3")) != "")  {
ordString += ", " + ordinal(Field("Num3"));
}
if (Trim(Field("Num4")) != "")  {
ordString += ", " + ordinal(Field("Num4"));
}
if (Trim(Field("Num5")) != "")  {
ordString += ", " + ordinal(Field("Num5"));
}

if (ordString.search(",") > -1)  {
var i = ordString.lastIndexOf(",");
slice1 = ordString.slice(0,i);
slice2 = ordString.slice(i+2,(ordString.length));
ordString = slice1 + " and " + slice2;
}
return ordString;```

You may, if you choose, put the ordinal function that I used into the global scripts area so that it may be used by other rules.

Good Luck,

##### Share on other sites

Hi David,

That code looks pretty good! I will try it out tonight and let you know.

One thing we might need to tweak ... with respect to adding "st" to any number that ends in 1 ... I think the only exception to this rule would be the number 11. (It would be 11th, not 11st)

I'll let you know what I come up with.

Cheers!

#### Archived

This topic is now archived and is closed to further replies.

×

• #### FusionPro.com

• MarcomCentral
×
• Create New...