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!

Script doesn't caluclate pass 1,000??

Status
Not open for further replies.

pinkpoppy

Programmer
Jul 6, 2011
75
US
I have this script that calculates numbers input by the user in txtboxPRICE and txtboxTAX and it automatically calculates the TOTAL in txtboxTOTAL. I tested it and it won't calculate pass $1,000? I need it to calculate numbers up to 25 digits.

Code:
          function formatCurrency(usFormat) {

              usFormat = usFormat.toString().replace(/\$|\,/g, '');

              if (isNaN(usFormat))

                  usFormat = "0";

              sign = (usFormat == (usFormat = Math.abs(usFormat)));

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

              cents = usFormat % 100;

              usFormat = Math.floor(usFormat / 100).toString();

              if (cents < 10)

                  cents = "0" + cents;

              for (var i = 0; i < Math.floor((usFormat.length - (1 + i)) / 3); i++)

                  usFormat = usFormat.substring(0, usFormat.length - (4 * i + 3)) + ',' + usFormat.substring(usFormat.length - (4 * i + 3));

              return (((sign) ? '' : '-') + '$' + usFormat + '.' + cents);

          }

Code:
          function CalTotal() {

              var PRICE= document.getElementById("<%=txtboxPRICE.ClientID %>").value;
              var TAX = document.getElementById("<%=txtboxTAX.ClientID %>").value;
              if (PRICE!= "" || TAX != "") {
                  if (TAX .substring(1) > PRICE.substring(1)) {
                      document.getElementById("<%=txtboxTOTAL.ClientID %>").value = PRICE.substring(1) - TAX .substring(1);
                  }
                  else {
                      document.getElementById("<%=txtboxTOTAL.ClientID %>").value = PRICE.substring(1) - TAX .substring(1);
                  }
              }
          }
 
I couldn't really understand what you were doing with the substrings given that either side of the if ... else had the same directive.

this code might do what you are looking for

Code:
<!DOCTYPE HTML>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>Test Document</title>
		<script type="text/javascript">
		  function CalTotal() {
              var PRICE= parseFloat(stripFormatting(document.getElementById("price").value));
			  var TAX = parseFloat(stripFormatting(document.getElementById("tax").value));
			  document.getElementById("total").value = formatCurrency(PRICE + TAX);
          } 
		  
		  function stripFormatting( num ){
		  		return num.substring(0,1) != '-'  ? num.replace(/[^0-9.]/g,'') : "-" + num.replace(/[^0-9.]/g,'');
		  }
		  
		  function formatCurrency( dollars ) {
              var sign = dollars < 0 ? "-" : "";
			  dollars = Math.abs(dollars).toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
			  return sign + "$" + dollars; // + "." + cents;  
          }
		</script>
	</head>
	<body>
		
		<form>
			Price: <input type="text" id="price"/><br/>
			Tax: <input type="text" id="tax"/><br/>
			Total: <input type="text" id="total"  /><br/>
			<button onclick="CalTotal(); return false;">Calculate</button>
		</form>
	</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top