Have an idea?

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

Perl Adding comma after every 1000's

Hi,

I have some calculation and want output with commas as thousands-separator

Here is the calculation

 <input name="Total_P" id="Total_P" type="hidden" value="

[% Begin Unverified Perl


my $TakeHome = 0;
$TakeHome  = (VALUE("calc_MP") * 36) + (VALUE("calc_M4P") * 48) + (VALUE("calc_UP"));

$TakeHome = ROUND($TakeHome / 1000) * 1000;

return $TakeHome;

End Unverified %]

">


I tried below code for comma separators but it doesn't work


 <input name="Total_P" id="Total_P" type="hidden" value="

[% Begin Unverified Perl


my $TakeHome = 0;
$TakeHome  = (VALUE("calc_MP") * 36) + (VALUE("calc_M4P") * 48) + (VALUE("calc_UP"));

$TakeHome = ROUND($TakeHome / 1000) * 1000;
$TakeHome = reverse ($TakeHome);
$TakeHome =~ s/(\d{3})(?=\d)(?!\d*\.)/$1,/g;
$TakeHome = scalar reverse $TakeHome;

return $TakeHome;

End Unverified %]

">


Please help me to correct the code
asked Sep 3, 2020 by Abdul Bronze (2,845 points)
Have you considered the SSI Script function ...

FORMATNUMBER (NUMBER,"ThousandsSeparator", "DecimalSeparator", NumDecimalPlaces)

Note: For all math functions, NUMBER can be any QuestionName that contains a numeric value or any numeric expression (including functions and mathematical operators) that returns a numeric value.
 
Example ...

FORMATNUMBER (Q1,",", ".",2)

This will set the Q1 response of of 1056.1234 as "1,056.12".

You can slot your calculation in where NUMBER is situated within the function.
Thanks Paul. It works.

1 Answer

0 votes
Good to see the FORMATNUMBER function did the job.

No need to reinvent what Sawtooth Software have already kindly provided.

Well done.
answered Sep 3, 2020 by Paul Moon Platinum (89,980 points)
...