Select to view content in your preferred language

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

8035
10
07-16-2018 09:59 AM
Emerging Contributor

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

Tags (3)
10 Replies
by
MVP Esteemed Contributor

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

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;‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``````
Emerging Contributor

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

Esri Esteemed Contributor

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
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);
}
‍‍‍‍‍‍``````
Regular Contributor

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

Esri Esteemed Contributor

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);
}
}
‍‍‍‍‍‍‍‍‍‍``````
Occasional Contributor
Esri Esteemed Contributor