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

Question

Question

Format currency field to accept up to 3 decimals

asked on May 30, 2017 Show version history

How do I get this to work? I have read several threads on how to format the currency field to accept additional digits after the decimal, e.g. $16.534. I have used this script:

$(document).ready(function () {
  $('.fourdecimal input').attr("pattern", '^(?:-?(?:0|[1-9][0-9]*)(?:\\.[0-9]{0,4})?)?$');
});

Even with that bit of code the form will not allow more than two digits to be input after the decimal point. Any assistance is appreciated.

0 0

Answer

SELECTED ANSWER
replied on June 4, 2017

Which version of Forms are you using? Since Forms 10.2.0 the decimal place for currency field varies based on currency format. For example currency "TND" supports 3 decimal places by default. And in 10.2.0 we added backend validation on currency decimal place so there is no way to modify it with custom script (the submitted value would be rounded.)

For Forms before 10.2.0, all currency formats have 2 decimal place, and you can modify it with script (update field id, it's 1 in the script):

$(document).ready(function(){
  $('#Field1').attr("pattern", '^(?:-?(?:0|[1-9][0-9]*)(?:\\.[0-9]{0,3})?)?$');
  _.find(LF.number.formObject.fields, {fieldId:1}).maxDecimalPlaces = 3;
})

 

0 0

Replies

replied on October 3, 2017

I tried what you suggested.  However I still get 'invalid input' on my form number field when using a decimal value.  

 

(version 9.2)

0 0
replied on October 8, 2017

Hi Ramika,

The above script only works for currency field;

If you need to change decimal place to 3 for number field in 9.2, you could use the following script:

$('#Field1').attr("step", '0.001');

Note that this will not work on Forms 10.2 and above.

0 0
replied on October 9, 2017

When I apply this setting the calculation doesn't work... ???

0 0
replied on October 9, 2017

Hi Ramika, what do you mean by calculation? I think there is no calculation (formula) in 9.2.

0 0
replied on October 10, 2017

Hi Rui, I have a function to calculate miles (whole or decimal number) * rate (3 digit number) = reimbursement (currency)

0 0
replied on October 10, 2017

Hi Ramika, did you use custom javascript for calculation? Can you post the whole script here?

0 0
replied on October 16, 2017
$(document).ready(function() {
    $('.mileage').addClass('lf-no-validate');
	$('.rate').addClass('lf-no-validate');
  	$('.cf-table-block').on('blur', 'input', sumtotal);
	$('.tax').on('blur', 'input', sumtotal);
	$('.shipping').on('blur', 'input', sumtotal);
  
	function sumtotal() {
		var sum = 0;
		$('.cf-table-block tbody tr').each(function () {
          	var s = 0.0;
			s = parseFloat($(this).find('.mileage input').val()) * parseFloat($(this).find('.rate input').val());
          	s = Math.round(s * 100)/100.0; //round to two decimal places
            $(this).find('.subtotal input').val(s);
            sum += s;
            sum += parseNumber($('.tax input').val()) + parseNumber($('.shipping input').val());
            			
		});
			$('.total input').val(sum);
	}
	function parseNumber(value) {
		var f = parseFloat(value); //Convert to float number
		return isNaN(f) ? 0 : f; //treat invalid input as 0;
	}
});

 

0 0
replied on October 16, 2017

So the number field is inside a table? If so, you need to set the field property when you add a new row on the table.

I tried adding the following script inside and it works well:

    $('.rate input').attr("step", '0.001');
    $('.cf-table-add-row').click(function(){
      $('.rate input').attr("step", '0.001');
    });

 

0 0
replied on October 17, 2017

Thank you... one question.  Where exactly does the above code go?  

0 0
replied on October 17, 2017

Anywhere inside the $(document).ready function, or add a new $(document).ready function, that doesn't matter.

1 0
replied on October 27, 2017

It worked!!!

0 0
replied on May 19, 2020

When I use this script it works in the form view but the variable saved with 2 decimal places. 

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

Sign in to reply to this post.