# Thread: currency converting help

1. Junior Coders
Join Date
Oct 2009
Posts
37
Thanks
8
Thanked 0 Times in 0 Posts

## currency converting help

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

HTML Code:
```<!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>
```
Last edited by shas1280; 05-16-2010 at 09:11 AM.

2. 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:

Code:
`  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:

Code:
```// 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.

3. ## The Following User Says Thank You to jscheuer1 For This Useful Post:

shas1280 (05-16-2010)

4. Junior Coders
Join Date
Oct 2009
Posts
37
Thanks
8
Thanked 0 Times in 0 Posts
Thanks alo.....t
gr8 yaa
u solved my problem simply

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•