Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Numbers which are Strings???

Status
Not open for further replies.

budapestnori

Technical User
Jan 15, 2004
33
HU
I hope this is simple for someone reading this. I have a web page - If you look at it, you will see a series of images, each image adjoining drop-downs with qty's for a particular print size for that a particular image. You will see I have it scripted so each image, has a running subtotal, to the bottom right of the dropdowns. Each time a print size qty for a given image is changed this Function is called:

function calculatePrintTotal(imgid)
{
var img4x6 = round_decimals((document.getElementById(imgid + "_4x6_cnt").value * .29),2);
var img5x7 = round_decimals((document.getElementById(imgid + "_5x7_cnt").value * .99),2);
var img8x10 = round_decimals((document.getElementById(imgid + "_8x10_cnt").value * 3.99),2);
var img16x20 = round_decimals((document.getElementById(imgid + "_16x20_cnt").value * 17.99),2);
var img20x30 = round_decimals((document.getElementById(imgid + "_20x30_cnt").value * 22.99),2);
var imgwallet = round_decimals((document.getElementById(imgid + "_wallet_cnt").value * 1.79),2);
document.getElementById("total_" + imgid).value = round_decimals((parseFloat(img4x6) + parseFloat(img5x7) + parseFloat(img8x10) + parseFloat(img16x20) + parseFloat(img20x30) + parseFloat(imgwallet)),2);
sumtotal();
}

It in turn calls two functions in its execution, one is called round_decimals and its code is here:

function round_decimals(original_number, decimals) {
var result1 = original_number * Math.pow(10, decimals)
var result2 = Math.round(result1)
var result3 = result2 / Math.pow(10, decimals)
return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

// Convert the number to a string
var value_string = rounded_value.toString()

// Locate the decimal point
var decimal_location = value_string.indexOf(".")

// Is there a decimal point?
if (decimal_location == -1) {

// If no, then all decimal places will be padded with 0s
decimal_part_length = 0

// If decimal_places is greater than zero, tack on a decimal point
value_string += decimal_places > 0 ? "." : ""
}
else {

// If yes, then only the extra decimal places will be padded with 0s
decimal_part_length = value_string.length - decimal_location - 1
}

// Calculate the number of decimal places that need to be padded with 0s
var pad_total = decimal_places - decimal_part_length

if (pad_total > 0) {

// Pad the string with 0s
for (var counter = 1; counter <= pad_total; counter++)
value_string += "0"
}
return value_string
}

The second function is giving me difficulties. Here it is:

function sumtotal(){
var sum = 0;
for(count = 1; count < rsArray.length; count++)
sum += parseFloat(sum + document.getElementById("total_"+rsArray[count]).value);
x = round_decimals(sum,2);
alert(x);
}

The rsArray is an arrray whose elements hold IDs for each image. In this function, you can see that I am attempting to loop through the element(s) named "total_rs"+Array[count] and store the total sum of their values into a variable called "sum". It is giving me screwy results, instead of a nice tidy "sum" I can then place in round_decimals and insert in another location on the page to give a shopper their grand total.

Any help would be appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top