Have an idea?

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

Using the ROUND function in Perl

I was using the ROUND function within some Perl Script as follows ...
[%Begin Unverified Perl 

 my $i=1;
 my $MD1Total=0;
 my $MD1Average=0;

 for($i=1; $i<=30; $i++)
  {
   $MD1Total=$MD1Total+VALUE("MD1_r1_c".$i)+VALUE("MD1_r2_c".$i);
  }

 $MD1Average=$MD1Total/60;
 
 SETVALUE("MD1AverageRating",ROUND($MD1Average,2));
 
End Unverified%]

I have 60 data points (saved in MD1_r1_c1 to MD1_r1_c30 and MD1_r2_c1 to MD1_r2_c30). It's a simple average calculation across the 60 data points that I am seeking. No problems there. It works fine.

To put the icing on the cake, I wanted to round the average to 2 decimal places (always). It almost works every time. I noticed if the average was 41.40, it only saved 41.4, and not 41.40. I suspect an average of 51.00 would only appear as 51?

Was hoping to find a neat solution where my average calculation always  provided me with the nearest 2 decimal places.
asked 5 days ago by Paul Moon Platinum (101,255 points)
edited 5 days ago by Paul Moon
I tested displaying to the screen and still not the desired reponse?
I suspect that Perl is reinterpreting it as a number before it's displayed. If you put the FormatNumber call in the survey I would think it would display correctly.

The same thing is probably happening in the Admin module, but you won't be able to add the FormatNumber function there.
I did try FormatNumber without success. I may have to format it outside LHS when providing output delivery?
I created a text variable and set it with this:

[% SetValue(var, FormatNumber(Round(12345, 2), "", ".", 2)) %]


In the admin module, I see "12345.00".
Hi Zachary, I was using ROUND on it's own, then FormatNumber on it's own, then I combined them together (as per your example). Still the same result.

I'm ok to live with it. It's not a disaster.

I may do some further tests with some other examples and see what I come up with.

Thanks to you and Walter for assisting just the same. Paul.

Your solution to the original question

Please only use this to answer the original question. Otherwise please use comments.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:

To avoid this verification in future, please log in or register.
...