ParseFloat stops calculating a decimal properly once it goes to a decimal point

SC10
Kilo Guru

I am a using a parseFloat to attempt to calculate two decimal fields, however once one of the two fields goes from 999 to 1,000, the calculation breaks.

My scripts:

function calculatetotalcosts() {

  var labour = parseFloat(g_form.getValue('u_estimateditlabourcosts'));

  var serviceandmaterials = parseFloat(g_form.getValue('u_estimatedserviceandmaterialscosts'));

  var result = labour + serviceandmaterials;

  g_form.setValue('u_estimatedtotalcosts',result);

}

Good calculation:

10 + 10 = 20

http://i.imgur.com/wxljW2F.png

Bad calculation:

1000 + 10 = 11

http://i.imgur.com/It06NTg.png

Looks to be that once it hits a decimal, it drops the zeroes and just sees the number before the decimal, 1 + 10 = 11.

Any suggestions?

1 ACCEPTED SOLUTION

edwin_munoz
Mega Guru

Helo Shane,



Most probably there is a comma in the string. Try it like this


  1. function calculatetotalcosts() {  
  2.   var labour = parseFloat(g_form.getValue('u_estimateditlabourcosts').replace(/,/g,''));  
  3.   var serviceandmaterials = parseFloat(g_form.getValue('u_estimatedserviceandmaterialscosts').replace(/,/g,''));  
  4.   var result = labour + serviceandmaterials;  
  5.   g_form.setValue('u_estimatedtotalcosts',result);  
  6. }  

View solution in original post

6 REPLIES 6

edwin_munoz
Mega Guru

Helo Shane,



Most probably there is a comma in the string. Try it like this


  1. function calculatetotalcosts() {  
  2.   var labour = parseFloat(g_form.getValue('u_estimateditlabourcosts').replace(/,/g,''));  
  3.   var serviceandmaterials = parseFloat(g_form.getValue('u_estimatedserviceandmaterialscosts').replace(/,/g,''));  
  4.   var result = labour + serviceandmaterials;  
  5.   g_form.setValue('u_estimatedtotalcosts',result);  
  6. }  

That did it, thanks.



Could you please break down what exactly that's doing?


Glad it worked.



Sure, when you do g_form.getValue you are getting a string like this: 10, 000. The replace method lets you replace characters on a string, The first parameter of the replace method is the character that is going to be replaced and the second one is for what it is going to be replaced. In this case /,/g is a regular expressions that basically searches for all occurrences of a comma ",". At the end all commas are replaced by ''.



Thanks.


I'm using these scripts on my dm_demand table, and have the fields dot walked on my Project form. Do you know how to make these scripts also apply to the fields that are dot walked to Demand, but are placed on my Project form?