- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-24-2015 12:53 PM
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?
Solved! Go to Solution.
- Labels:
-
User Interface (UI)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-24-2015 01:25 PM
Helo Shane,
Most probably there is a comma in the string. Try it like this
- function calculatetotalcosts() {
- var labour = parseFloat(g_form.getValue('u_estimateditlabourcosts').replace(/,/g,''));
- var serviceandmaterials = parseFloat(g_form.getValue('u_estimatedserviceandmaterialscosts').replace(/,/g,''));
- var result = labour + serviceandmaterials;
- g_form.setValue('u_estimatedtotalcosts',result);
- }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-24-2015 01:25 PM
Helo Shane,
Most probably there is a comma in the string. Try it like this
- function calculatetotalcosts() {
- var labour = parseFloat(g_form.getValue('u_estimateditlabourcosts').replace(/,/g,''));
- var serviceandmaterials = parseFloat(g_form.getValue('u_estimatedserviceandmaterialscosts').replace(/,/g,''));
- var result = labour + serviceandmaterials;
- g_form.setValue('u_estimatedtotalcosts',result);
- }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-24-2015 02:12 PM
That did it, thanks.
Could you please break down what exactly that's doing?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-24-2015 02:33 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-24-2015 03:36 PM
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?