Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Adding comma and space for values of 1000+

I have a variable "cost" that appears as an element of an attribute in a CBC. Its value is given by code below:

<br><br><b>[% begin unverified perl  
my $cost = 500+ROUND((VALUE("lrr7")-1)*60000/5)*(1+(VALUE("income")-6)*VALUE("incper"));
my $avg = “Cost to avoid illness: £”;
my $comb = $avg . $cost;
SETVALUE("lrc7",$cost);
return $comb;
end unverified  %]</b> 

 
What appears in the CBC is a combination of the text "Cost to avoid illness: £” and the numerical value.
 
I would like there to be a comma and space for values >£999:
£12
£123
£1, 234
£12, 234
£122, 234

But attempts to code this have failed –help appreciated.   
 
The cost will never reach £1 million(!) and there will never be decimal places
 
Thanks
Dan
asked Oct 18, 2016 by dan r Bronze (2,050 points)

2 Answers

0 votes
Hi Dan,

You can do this using regular expressions and Perl's "reverse" function:

[% Begin Unverified Perl
my $cost = 500+ROUND((VALUE("lrr7")-1)*60000/5)*(1+(VALUE("income")-6)*VALUE("incper"));
SETVALUE("lrc7", $cost);
$cost = reverse $cost;
$cost =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1 ,/g;
$cost = scalar reverse $cost;
my $str = 'Cost to avoid illness: £' . $cost;
return $str;
End Unverified %]
answered Oct 18, 2016 by Zachary Platinum Sawtooth Software, Inc. (126,575 points)
Thanks Zachary, this worked for us

Dan
0 votes
This works for me.

my $value  = reverse ($value);
$value =~ s/(\d{3})(?=\d)(?!\d*\.)/$1,/go;
$value = reverse $value;
answered Oct 18, 2016 by Mike Lodder Gold (23,395 points)
...