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

Question

Question

Set decimal value in currency fields

asked on October 27, 2015

Hi,

I am trying to set decimal value in currency field, but i keep breaking the script. 

This is the current script, i have tried to insert .toFixed(2) 

$(function() {
  $(document).ready(function () {
    
    
    function parseNumber(n) {
      var f = parseFloat(n); //Convert to float number.
      return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
    function sumtotal() {
      var s = 0;
      var s1 = 0;
      var s2 = 0;
      var s3 = 0;
      
      
      $('.cf-table-block tbody tr').each(function () {
        
        s += parseNumber($(this).find('.estc1 input').val());
        s1 += parseNumber($(this).find('.estc2 input').val());
        s2 += parseNumber($(this).find('.estc3 input').val());
        s3 += parseNumber($(this).find('.estc4 input').val());
        

                 

      });
      
      
       $('.total input').val(s + s1 + s2 +s3);
      
      
     }
     $('.cf-table-block').change(sumtotal);
    });
});
    

Also tried to enter 

 s = s.toFixed(2);
 s1 = s1.toFixed(2);
 s2 = s2.toFixed(2);
 s3 = s3.toFixed(2);

and the same for the total. What am i doing wrong?

0 0

Answer

SELECTED ANSWER
replied on October 27, 2015

Hi Ming-Iun,

I came right with the code. I just added the .toFixed(2) after calculations were done. I want it to convert the total currency field to 2 decimal.

0 0

Replies

replied on October 27, 2015

Hello Gert,

 

Just to clarify, are you trying to format the value of the currency field to accept two decimals and round up to the hundredth place? If that's the case, the JavaScript on this Answers post that should give you a good start. Hope this helps!

0 0
SELECTED ANSWER
replied on October 27, 2015

Hi Ming-Iun,

I came right with the code. I just added the .toFixed(2) after calculations were done. I want it to convert the total currency field to 2 decimal.

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

Sign in to reply to this post.