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

Question

Question

Make sure there are two decimal places

asked on November 19, 2015

I have a form that needs a total that has two decimal places. We would like it to have two decimal spots no matter what. For example below, we'd like to be .10 instead of .1

I'm not sure if there is a simple fix for this or if something more is needed. Any help is really appreciated.

Thanks!

0 0

Answer

SELECTED ANSWER
replied on November 20, 2015

You probably have a line of code in your JavaScript that looks like

$('.total input').val(sum);

Or something to that effect. You just need to apply the toFixed method to that value that you are setting as the total. Change the line of code to look like

$('.total input').val(sum.toFixed(2));
2 0

Replies

replied on November 19, 2015

You can use the toFixed method and apply that to the value that you ultimately set into the "Currency" field.

0 0
replied on November 19, 2015

Would I place the variable below? Or can I place it in any other coding?

var n = num.toFixed(2);

I'm just not good with writing out code.

0 0
replied on November 19, 2015

For example, if your Javascript uses a variable called "sum" to calculate the total cost from your table and you are going to set that "sum" value into the Currency field (assuming that Currency field uses the CSS class name currency) then you can do something like

$('.currency input').val(sum.toFixed(2));
1 0
replied on November 20, 2015

Right now we're using the css class for sum with the "cost" field in the table. For the Total field, outside the table, we're using a css class for "total." How would I change the code to to make sure it worked?

 

My other question is can I set this code as it's own line? Or do I need to place it inside other coding I have?

0 0
SELECTED ANSWER
replied on November 20, 2015

You probably have a line of code in your JavaScript that looks like

$('.total input').val(sum);

Or something to that effect. You just need to apply the toFixed method to that value that you are setting as the total. Change the line of code to look like

$('.total input').val(sum.toFixed(2));
2 0
replied on November 20, 2015

That worked! Thank you so much for your help, Alexander!

0 0
replied on November 30, 2015

Alex,

I had to make some changes for our accounting department on this form. They wanted to make sure the numbers included a comma, so I switched the fields to Number and Single line for both parts.

Having done that, the total field seems to be doubling what is put into the sum field.

Do you know why it would be doing that and what change I need to make?

0 0
replied on November 30, 2015

Are you only seeing this doubling issue in IE? Try changing

$(this).find('input').each(function () {

to

$(this).find('input').filter(':visible').each(function () {

Also change

$(this).find('.sum input').each(function () {

to

$(this).find('.sum input').filter(':visible').each(function () {

0 0
replied on November 30, 2015

That worked, but I checked to see if it's adding commas and it looks like it's holding the total to just 2 digits and then 2 decimal spaces now.

0 0
replied on November 30, 2015

For comma usage, you may want to refer to the suggestions from this thread or perhaps consider upgrading to Forms 10 where number fields now have built in options for decimal places and using the thousands delimiter.

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

Sign in to reply to this post.