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

Question

Question

Round to 2 decimals doesn't work for tables

asked on January 4, 2017 Show version history

In order to more easily manage forms and restrict currency fields to two decimals, I have been using the following code:

$(document).ready(function() {

  $('.currency').change(function() {
    $(this).val(formatCurrency($(this).val()));
  });
  
  function formatCurrency(value) {
    var float = parseFloat(value);
    return isNaN(float) ? 0 : (Math.round(float*100)/100).toFixed(2);
  }
});

I found it in this thread: https://answers.laserfiche.com/questions/89089/Round-to-2-decimal-points-with-Forms-10.  

This code only restricts the currency fields on the first row of a table to two decimals.  All subsequent rows with a currency values are not restricted to two decimals.  Any suggestions on how to fix this?

0 0

Answer

APPROVED ANSWER
replied on January 5, 2017

Hi Jen,

Try this:

$(document).ready(function() {

  $('.currency').change(function() {
    $(this).val(formatCurrency($(this).val()));
  });
  
  $('.cf-table-add-row').click(function(){
    $('.currency').change(function() {
      $(this).val(formatCurrency($(this).val()));
    });
  });
  
  function formatCurrency(value) {
    var float = parseFloat(value);
    return isNaN(float) ? 0 : (Math.round(float*100)/100).toFixed(2);
  }
});
1 0

Replies

replied on January 4, 2017

change the second line to:

$(".currency").on('change', 'input', function() {

I'm assuming you are dynamically adding in rows. The previous function will only run on elements that currently exist on the web page, so you need to listen for newly created rows.

1 0
replied on January 5, 2017

Hi Xavier

I've used this with a regular number field to prepend a $ to a number field to make it look like a currency field, thus allowing the field to have separators and a set decimal length native to the field.

This code works in a standalone field but not in a table. how would you change it to work in a table where you could add dynamic fields?

$(document).ready(function() {

  $('.moneyclass .cf-field').prepend('<span class="margin-end">$</span>');

});

0 0
replied on January 5, 2017

You would need to listen to row creation

$(".sampleTableClass").on("change", ".currency", "input", function(){});

 

0 0
replied on January 5, 2017

Hi Xavier, think I'm missing something here. I have added the sampeTableClass to the Table and revised the code as below but having no success.

What does the ".currency" stand for as I don't have a currency field, but am using a number field in it's place?  The Number field has the class assigned "moneyclass"

$(".sampleTableClass").on("change", ".currency", "input", function(){

  $('.moneyclass .cf-field').prepend('<span class="margin-end">$</span>');

});

0 0
replied on January 5, 2017

".currency" is just the class of the field of interest. Replace it with your moneyclass

0 0
replied on January 5, 2017

I thought that's what it was and had tried that but it doesn't work

$(".sampleTableClass").on("change", ".moneyclass", "input", function(){

  $('.moneyclass .cf-field').prepend('<span class="margin-end">$</span>');

});

0 0
replied on January 5, 2017 Show version history

Hi  Xavier, I changed the code to this: 

$(document).ready(function() {

$('.currency').on('change', 'input', function() {
    $(this).val(formatCurrency($(this).val()));
  });
  
  function formatCurrency(value) {
    var float = parseFloat(value);
    return isNaN(float) ? 0 : (Math.round(float*100)/100).toFixed(2);
  }
});

After making this change, none of the currency fields in the table have two decimals.

0 0
APPROVED ANSWER
replied on January 5, 2017

Hi Jen,

Try this:

$(document).ready(function() {

  $('.currency').change(function() {
    $(this).val(formatCurrency($(this).val()));
  });
  
  $('.cf-table-add-row').click(function(){
    $('.currency').change(function() {
      $(this).val(formatCurrency($(this).val()));
    });
  });
  
  function formatCurrency(value) {
    var float = parseFloat(value);
    return isNaN(float) ? 0 : (Math.round(float*100)/100).toFixed(2);
  }
});
1 0
replied on January 5, 2017

That works great.  Thanks, Alex!!

0 0
replied on January 17, 2017

Hi Jen,

FYI, Forms 10.2 now defaults the currency fields to display 2 decimal places and round up or down by default when it is a calculated field. 

Doesn't fix your problem above but thought you'd be interested to know.

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

Sign in to reply to this post.