Draygon

05-28-2009, 03:47 AM

I am new to Javascript and have a really simple script that I have wrote that allows users to answer two separate questions and then hit a calculate button and it some some simple math.

That part is working, a working example is here Calculate Example (www.omgmod.com/calculate.html)

and the code is :

<html>

<head>

<script type="text/javascript">

function Calculate() {

var amount = parseInt(document.getElementById('amount').value);

var customers = parseInt(document.getElementById('customers').value);

var monthly = amount * customers;

document.getElementById('monthly').textContent = monthly;

document.getElementById('monthly').innerText = monthly;

document.getElementById('yearly').textContent = 12 * monthly;

document.getElementById('yearly').innerText = 12 * monthly;

return false;

}

</script>

</head>

<body>

<form method="post" action="" onsubmit="return Calculate();">

Enter the amount of your average customer transaction: <input type="text" id="amount" name="amount" /><br />

Enter the number of new customers per week you could get: <input type="text" id="customers" name="customers" /><br />

<input type="submit" name="submit" value="Calculate" /><br />

Additional monthly revenue: $<span id="monthly"></span><br />

Additional yearly revenue: $<span id="yearly"></span><br />

</form>

</body>

</html>

The problem I have now is that when someone puts a comma in the input box it ignores everything after the comma and if someone puts in a decimal number it rounds it up instead of using the decimal in the calculations for the output.

I found online a script for formatting numbers in commas and this is it:

function addCommas(nStr)

{

nStr += '';

x = nStr.split('.');

x1 = x[0];

x2 = x.length > 1 ? '.' + x[1] : '';

var rgx = /(\d+)(\d{3})/;

while (rgx.test(x1)) {

x1 = x1.replace(rgx, '$1' + ',' + '$2');

}

return x1 + x2;

}

Could someone help me out and explain to me how to use the formatting function to format the output with commas and allow commas to be used for the input as well; as well as how to get it to do proper math with the decimal and not ignore it?

Appreciate any help here.

That part is working, a working example is here Calculate Example (www.omgmod.com/calculate.html)

and the code is :

<html>

<head>

<script type="text/javascript">

function Calculate() {

var amount = parseInt(document.getElementById('amount').value);

var customers = parseInt(document.getElementById('customers').value);

var monthly = amount * customers;

document.getElementById('monthly').textContent = monthly;

document.getElementById('monthly').innerText = monthly;

document.getElementById('yearly').textContent = 12 * monthly;

document.getElementById('yearly').innerText = 12 * monthly;

return false;

}

</script>

</head>

<body>

<form method="post" action="" onsubmit="return Calculate();">

Enter the amount of your average customer transaction: <input type="text" id="amount" name="amount" /><br />

Enter the number of new customers per week you could get: <input type="text" id="customers" name="customers" /><br />

<input type="submit" name="submit" value="Calculate" /><br />

Additional monthly revenue: $<span id="monthly"></span><br />

Additional yearly revenue: $<span id="yearly"></span><br />

</form>

</body>

</html>

The problem I have now is that when someone puts a comma in the input box it ignores everything after the comma and if someone puts in a decimal number it rounds it up instead of using the decimal in the calculations for the output.

I found online a script for formatting numbers in commas and this is it:

function addCommas(nStr)

{

nStr += '';

x = nStr.split('.');

x1 = x[0];

x2 = x.length > 1 ? '.' + x[1] : '';

var rgx = /(\d+)(\d{3})/;

while (rgx.test(x1)) {

x1 = x1.replace(rgx, '$1' + ',' + '$2');

}

return x1 + x2;

}

Could someone help me out and explain to me how to use the formatting function to format the output with commas and allow commas to be used for the input as well; as well as how to get it to do proper math with the decimal and not ignore it?

Appreciate any help here.