Answered by:
How to get the sum of array elements

Question
-
Hi all,
i was trying to get the sum of array values retrieved by a xml query, so i have used the following code:
var doc = new ActiveXObject("MSXML2.DOMDocument");
doc.async = false;
doc.loadXML(resultXml);
var m = doc.selectNodes("//q1:manualdiscountamount");
var totalLineItemDiscountAmount=0;
if(m!=null)
{
for(i=0;i<m.length;i++)
{
alert("manualdiscountamount"+m[i].text);
totalLineItemDiscountAmount+=m[i].text;
}
}
alert("the sum is:"+totalLineItemDiscountAmount);
but for some reason that i cannot understand, this code concatenate the elements !!!
could anybody please help me to modify THIS code so i can get the summation of array values
any help will be appreciated
thanks in advance
Monday, November 15, 2010 10:50 PM
Answers
-
You need to use parseFloat()
totalLineItemDiscountAmount+=parseFloat(m[i].text);
MSCRM Bing'd - http://bingsoft.wordpress.com Check out the CRM 4 to CRM 2011 JavaScript Converter Tool CRM Forum Guidance on how to Help Us Help You - Proposed as answer by RhettClintonMVP, Moderator Monday, November 15, 2010 11:09 PM
- Marked as answer by Donna EdwardsMVP Friday, November 19, 2010 8:01 PM
Monday, November 15, 2010 11:09 PMModerator -
Odd, i though parse Decimal existed. to get the fixed decimal try
totalLineItemDiscountAmount.toFixed(2);
http://www.w3schools.com/jsref/jsref_tofixed.asp
MSCRM Bing'd - http://bingsoft.wordpress.com Check out the CRM 4 to CRM 2011 JavaScript Converter Tool CRM Forum Guidance on how to Help Us Help You - Marked as answer by Donna EdwardsMVP Friday, November 19, 2010 8:01 PM
Tuesday, November 16, 2010 1:03 AMModerator
All replies
-
You need to use parseFloat()
totalLineItemDiscountAmount+=parseFloat(m[i].text);
MSCRM Bing'd - http://bingsoft.wordpress.com Check out the CRM 4 to CRM 2011 JavaScript Converter Tool CRM Forum Guidance on how to Help Us Help You - Proposed as answer by RhettClintonMVP, Moderator Monday, November 15, 2010 11:09 PM
- Marked as answer by Donna EdwardsMVP Friday, November 19, 2010 8:01 PM
Monday, November 15, 2010 11:09 PMModerator -
Mostafa,
You keep posting the same questions in both the 'CRM' and 'CRM Development' forums; for those of us that frequent both, it's confusing and unnecessarily redundant.
Please try to choose the most appropriate forum; if you are unable to find the answers you are looking, and/or if another forum is deemed more appropriate, you post can/will be moved.
--pogo (pat)Monday, November 15, 2010 11:49 PM -
Thanks alot guys, i've used parseFloat and it works, the parseDecimal don't !!
but what if i need to display numbers with fractions ??
for exampe if the summation is 15, i want to display it as 15.00
Tuesday, November 16, 2010 12:56 AM -
Odd, i though parse Decimal existed. to get the fixed decimal try
totalLineItemDiscountAmount.toFixed(2);
http://www.w3schools.com/jsref/jsref_tofixed.asp
MSCRM Bing'd - http://bingsoft.wordpress.com Check out the CRM 4 to CRM 2011 JavaScript Converter Tool CRM Forum Guidance on how to Help Us Help You - Marked as answer by Donna EdwardsMVP Friday, November 19, 2010 8:01 PM
Tuesday, November 16, 2010 1:03 AMModerator