Have an idea?

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

Show decimals in numeric question

I have a numeric question that is set to allow decimals, but my client may want it to force this format, $00.00.  Is there a way to require/show two decimals?
asked Oct 1 by Jay Rutherford Gold (45,095 points)
What does "force" mean here?  You want two decimals appearing all the time?  Or just that responses aren't accepted without exactly two decimal places?
At this point I believe two decimals appearing all the time.
That's possible, but it's not trivial.  You may want to look up JavaScript mask libraries and see if one satisfies your client and meets any licensing requirements, then we can hook up that library with Lighthouse Studio.
Unfortunately time didn't allow me to find what I needed to force two decimals.  Of course now I have another project that is asking for something similar (entering a dollar amount to include cents).

Curious if you have any easy way to not allow more than two decimals?

1 Answer

0 votes
 
Best answer
Sure, try this custom JavaScript verification:

if (/\.\d{3}/.test(SSI_GetValue('[% QuestionName() %]'))) {
    strErrorMessage = 'error...';
}
answered Oct 6 by Zachary Platinum Sawtooth Software, Inc. (188,000 points)
selected Oct 6 by Jay Rutherford
That works great to stop more than two decimals.  I'm assuming the 3 in the script is looking at the number of decimals?  While I originally was asking about always 'Showing' a specific format, can you adjust this error script to always look for two decimals?
Yes, that is a regular expression looking for three numbers after a period.

So every response must have exactly two decimal places?
Well, if it's possible to just show the error if there is only 1 decimal or more than two, that would be great.  I would think allowing a whole number with no decimals would be okay with my client.
Ah, I see.

if (/\.\d$|\.\d{3}/.test($('#[% QuestionName() %]').val().trim())) {
    strErrorMessage = 'error...';
}
Ahh, that's beautiful!  This way it guarantees that I'm getting a valid monetary response, either a whole number or two decimals for cents.  This will definitely do the trick.  Thanks as always Zach!
Zach, I discovered a problem with this one.  if I enter two decimals ending with a 0 (i.e. 7.50) it triggers the error script.  Any way around this?
Oh right, we can't use SSI_GetValue if trailing zeroes need to be supported.  I've edited my earlier script.
So I got that working on the single numeric question.  However, I have a grid question with a constructed row list that needed the same logic.  I had it working with the previous script, but can't seem to get this new modification to work with what I had.  Here is what I'm trying:
for (var i = 1; i <= 19; i++) {
if (/\.\d$|\.\d{3}/.test($('#[% QuestionName() %]_r' + i + '_c1').val().trim())) {
    strErrorMessage = 'Your response should be in this format $00.00';
}
}
Does your grid question involve constructed lists?
It does, yes.  The rowlist is constructed.
We could change up the regex test line, but my usual is to read the constructed list items and write the loop like this:

var rows = $('input[name="hid_row_list_[% QuestionName() %]"]').val().split(',');
rows.forEach(function(row){
    if (/\.\d$|\.\d{3}/.test($('#[% QuestionName() %]_r' + row + '_c1').val().trim())) {
        strErrorMessage = 'Your response should be in this format $00.00';
    }
});
Okay, that is working.  Nice solution.  So what I did would have worked if it wasn't constructed lists then?
Yeah.
...