Arcade expression for formatting a number into Station+ (Plus) format in WebMap Popup

2563
10
07-16-2018 09:59 AM
Highlighted
New Contributor II

Arcade expression for formatting a number into Station+ (Plus) format in WebMap Popup

I am trying to the equivalent Arcade expression for the following VBScript: 

Function FindLabel ( [Station] )
if (LEN(ROUND([Station],0))) = "7" then
FindLabel = LEFT(ROUND([Station],0),5) + "+" + RIGHT(ROUND([Station],0),2)
elseif (LEN(ROUND([Station],0))) = "6" then
FindLabel = LEFT(ROUND([Station],0),4) + "+" + RIGHT(ROUND([Station],0),2)
elseif (LEN(ROUND([Station],0))) = "5" then
FindLabel = LEFT(ROUND([Station],0),3) + "+" + RIGHT(ROUND([Station],0),2)
elseif (LEN(ROUND([Station],0))) = "4" then
FindLabel = LEFT(ROUND([Station],0),2) + "+" + RIGHT(ROUND([Station],0),2)
elseif (LEN(ROUND([Station],0))) = "3" then
FindLabel = LEFT(ROUND([Station],0),1) + "+" + RIGHT(ROUND([Station],0),2)
elseif (LEN(ROUND([Station],0))) = "2" then
FindLabel = LEFT(ROUND([Station],0),0) + "0+" + RIGHT(ROUND([Station],0),2)
elseif (LEN(ROUND([Station],0))) = "1" then
FindLabel = LEFT(ROUND([Station],0),0) + "0+0" + RIGHT(ROUND([Station],0),2)
else
FindLabel = ROUND([Station],0)
end if
End Function

It appears there is not an Arcade text/string LENGTH function to be able to return the number of characters.  Is there another way to format a number with varying digits/characters to take the last 2 digits for the record and insert a "+" before those 2 digits.

Example

VALUE      STATION

639249      6392+49

11715        117+15

234            2+34

Reply
0 Kudos
10 Replies
Highlighted
MVP Honored Contributor

The Count function returns how many characters are in a string.

Highlighted
Esri Esteemed Contributor

Try this:

var station = Round($feature.Station, 0);
var len = Count(Text(station));

var start = "";
var middle = "";
var end = "";
if (len > 7) {
    start = station;
    middle = "";
    end = "";
} else if (len > 1) {
    start = Left(station, len-2);
    middle = "+";
    end = Right(station, 2);
} else if (len == 1) {
    start = "";
    middle = "0+";
    end = Right(station, 1);
} else {
    start = "";
    middle = "0+0";
    end = "";
}

var label = start + middle + end;
return label;
Highlighted
New Contributor II

I used this one:

var station = $feature.BeginStation;
var len = Count(Text(station))
var end = Text(station, "00.00")
var labelStation =""
var leftVal = len - 5
var start = Left(station, leftVal)
if (start=="") {
start="0";
}
labelStation = start + '+' + end

return labelStation

Reply
0 Kudos
Highlighted
Esri Esteemed Contributor

Hi Steven Weaver ,

Are you sure your expression is correct? The OP provided some values and desired results and when I use your expression they don't match the desired result:

Value OPDesired STATION OPSTATION with your expression
6392496392+496+49.00
11715117+150+15.00
2342+340+34.00
Reply
0 Kudos
Highlighted
Esri Esteemed Contributor

The expression for the OP could probably be shortened to something like this:

var value = $feature.Station;
if (Count(Text(value))<3) {
return Left(Text(value, "000"), Count(Text(value, "000"))-2) + "+" + Right(Text(value, "000"), 2);
} else {
return Left(Text(value), Count(Text(value))-2) + "+" + Right(Text(value), 2);
}
Highlighted
New Contributor III

Hi Xander,

I'm trying to add in an "else if" to your code here to return and empty value if the field is blank instead of "+". I added in:

} else if (value=='') {
return "";

When the field is empty, it is still coming back as "+" instead of "". I also tried value==' ' to see if there was a space being saved as a blank but it was still returning "+". Do you have an idea of how I could get this to work?

Thanks!

Tim

Reply
0 Kudos
Highlighted
Esri Esteemed Contributor

Hi twoodfield_DawoodGIS ,

You could probably do something like this:

var value = $feature.Station;
if (IsEmpty(value)) {
return "";
} else {
if (Count(Text(value))<3) {
return Left(Text(value, "000"), Count(Text(value, "000"))-2) + "+" + Right(Text(value, "000"), 2);
} else {
return Left(Text(value), Count(Text(value))-2) + "+" + Right(Text(value), 2);
}
}
Highlighted
New Contributor II
Highlighted
Esri Esteemed Contributor

Hi jgrossman_VHB ,

Glad to hear that it was helpful!

Reply
0 Kudos