You are viewing limited content. For full access, please sign in.

Question

Question

Currency field in Forms

asked on May 1, 2014 Show version history

Hello,

 

I wanted to know if there was a way to have the field formatted to drop the change, and just display the dollar amount.  I'd also like to know if there's a way to add in a comma when appropriate.  

 

I realize that this will probably take some scripting outside of the feature set of forms.  

 

Thanks for your assistance! 

0 0

Answer

APPROVED ANSWER SELECTED ANSWER
replied on May 2, 2014 Show version history

Here's the same code slightly modified to also round the value before adding commas.

 

$(document).ready(function () {
  $('.addCommas input').blur(function () {
    $(this).val(Math.round($(this).val()));
    $(this).val($(this).val().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));
  });  
});

It's important to note that, after the commas are inserted, JavaScript won't recognize the value as a number (it's a string at that point). So, just keep that in mind if you're performing calculations or if this value is updated multiple times.

0 0

Replies

replied on May 1, 2014

Do you want to round to the nearest dollar, or just cut the change off entirely?

0 0
replied on May 1, 2014

We were just thinking cut it off.  That way the person can decide if they want to round up or not.  

0 0
replied on May 1, 2014

Do you mean you want to restrict entry to only show two decimal places, like $35.50? Or would you rather that show $35?

0 0
replied on May 1, 2014

For your second request, add the addCommas class to the field where you're capturing the value. It's best that this field is a single line field so it will accept the commas without complaining.

 

On the CSS and JavaScript page, insert the following code in the JavaScript section.

 

$(document).ready(function () {
  $('.addCommas input').blur(function () {
 $(this).val($(this).val().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));
  });  
});

Of course, if you're using this in combination with other JavaScript (to calculate totals, for example) you might have to do a little extra work.

0 0
replied on May 2, 2014

Thanks for the comma info Eric!  

 

So the client is kind of changing their mind... How would you round to the nearest dollar up or down?   

0 0
APPROVED ANSWER SELECTED ANSWER
replied on May 2, 2014 Show version history

Here's the same code slightly modified to also round the value before adding commas.

 

$(document).ready(function () {
  $('.addCommas input').blur(function () {
    $(this).val(Math.round($(this).val()));
    $(this).val($(this).val().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"));
  });  
});

It's important to note that, after the commas are inserted, JavaScript won't recognize the value as a number (it's a string at that point). So, just keep that in mind if you're performing calculations or if this value is updated multiple times.

0 0
replied on May 5, 2014

Thanks again Eric.  These work great.  

0 0
replied on May 5, 2014

You're welcome!

0 0
replied on May 27, 2015

Is there anyway to get this to work with existing currency fields on a form?

We have a lot of currency fields and I would rather not go in and change them all to single line fields and then update all the variables that are already being used in the Forms process and subsequent workflows.

 

Thanks in advance,

Eric R.

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.