PDA

View Full Version : Resolved currency converting help



shas1280
05-16-2010, 07:53 AM
Hi..
i have a checkbox value calculator.but i didnt get the correct value from the check box when am selecting a checkbox.
EX: my 1st check box value is 100.125(One hudred and twentyfive) but when i selecting the checkbox iam getting 100.13,(One hudred and thirty) this means the checkbox value is not giving the last 3 digits and its converting into the next 2 digits value. how can i solve this, anybody can help me.

here is the html example what i mean




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>

<script type="text/javascript">
<!--
/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Kevin Hartig :: http://www.grafikfx.net/ */

// Calculate the total for items in the form which are selected.
function calculateTotal(inputItem) {
with (inputItem.form) {
// Process each of the different input types in the form.
if (inputItem.type == "radio") { // Process radio buttons.
// Subtract the previously selected radio button value from the total.
calculatedTotal.value = eval(calculatedTotal.value) - eval(previouslySelectedRadioButton.value);
// Save the current radio selection value.
previouslySelectedRadioButton.value = eval(inputItem.value);
// Add the current radio button selection value to the total.
calculatedTotal.value = eval(calculatedTotal.value) + eval(inputItem.value);
} else { // Process check boxes.
if (inputItem.checked == false) { // Item was uncheck. Subtract item value from total.
calculatedTotal.value = eval(calculatedTotal.value) - eval(inputItem.value);
} else { // Item was checked. Add the item value to the total.
calculatedTotal.value = eval(calculatedTotal.value) + eval(inputItem.value);
}
}

// Total value should never be less than 0.
if (calculatedTotal.value < 0) {
InitForm();
}

// Return total value.
return(formatCurrency(calculatedTotal.value));
}
}

// Format a value as currency.
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3)) + ',' + num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + 'KD' + num + '.' + cents);
}

// This function initialzes all the form elements to default values.
function InitForm() {
// Reset values on form.
document.selectionForm.total.value='';
document.selectionForm.calculatedTotal.value=0;
document.selectionForm.previouslySelectedRadioButton.value=0;

// Set all checkboxes and radio buttons on form to unchecked.
for (i=0; i < document.selectionForm.elements.length; i++) {
if (document.selectionForm.elements[i].type == 'checkbox' | document.selectionForm.elements[i].type == 'radio') {
document.selectionForm.elements[i].checked = false;
}
}
}

//-->
</script>
<style type="text/css">
<!--
.wrapper {
height: 150px;
width: 700px;
margin-top: 50px;
margin-left: 50px;
}

.discription {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 18px;
font-weight: lighter;
color: #000000;
height: 25px;
width: 530px;
overflow: hidden;
position: relative;
border: 1px solid #000000;
float: left;
}
.checkbox {
float: left;
height: 26px;
width: 24px;
overflow: hidden;
position: relative;
text-align: center;
}
.item {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 18px;
font-weight: lighter;
color: #000000;
float: left;
width: 120px;
height: 25px;
border: 1px solid #000000;
text-align: center;
}
-->
</style>
</head>

<body>
<div class="wrapper">
<form id="myform">
<div class="discription">Item1</div>
<div class="checkbox"><input name="Business cards, standard*" value="100.125"
onclick="this.form.total.value=calculateTotal(this);" type="checkbox"></div>
<div class="item">100.125</div>
<div class="discription">Item2</div>
<div class="checkbox"><input name="Business cards, international****" value="100.125"
onclick="this.form.total.value=calculateTotal(this);" type="checkbox" id="Business cards, international****"></div>
<div class="item">100.125</div>
<div class="discription">Item3</div>
<div class="checkbox"> <input name="Business cards, monarch*****" value="100.020"
onclick="this.form.total.value=calculateTotal(this);" type="checkbox" id="Business cards, monarch*****"></div>
<div class="item">100.20</div>
<div class="discription" style="background-color:#ff99cc"></div>
<div class="checkbox"></div>
<div class="item"style="background-color:#ff99cc; border-bottom:1px solid #000000;border-top: 1px solid #000000; height:25px"><input name="calculatedTotal" value="0" type="hidden">
<input name="previouslySelectedRadioButton" value="0" type="hidden">
<input name="total" readonly="readonly"
onfocus="this.blur();" type="text" size="10" style="float:center; background-color:#ff99cc;"></div>
</form>


</div>
</body>
</html>

jscheuer1
05-16-2010, 08:39 AM
Well, in currency transactions there usually only are two decimal (cents) places. And, if there are more, these are usually rounded up or sometimes simply rounded. Your code appears to round up. Here is where it does so:


num = Math.floor(num*100+0.50000000001);

That will make 100.125 into 10013. Later division by 100 turns it back to 100.13.

I can't be certain exactly what you are after, but try this substitute:


// Format a value as currency.
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = num.toFixed(3).replace(/(\.\d\d)0+$/, '$1');
return (((sign)?'':'-') + 'KD' + num);
}

for your current formatCurrency function.

shas1280
05-16-2010, 09:07 AM
Thanks alo.....t
gr8 yaa
u solved my problem simply