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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rounding number fields to two decimal places...

Status
Not open for further replies.

Olorin1

Technical User
Feb 14, 2001
17
CA
Hi there,

I need to have my form round my calculated totals and tax field to .00 (two decimal places only). I understand that it isn't easy to do, due to a JS bug with Math.round

It is giving me quite a headache.%-( Does anyone know how to do this? Help!!!! :)

this what i've tried:
<code>
<script language=&quot;javascript&quot;>
function format (expr, decplaces) {
var str = &quot;&quot; + Math.round (eval(expr) * Math.pow(10,decplaces))
while (str.length <= decplaces){
str = &quot;0&quot; + str
}
var decpoint = str.length - decplaces
return str.substring(0, decpoint) + &quot;.&quot; + str.substring(decpoint,str.lenght);
}

function dollarize (expr){
return &quot;$&quot; + format(expr,2)
}
</script>
</code>

I tried changing the Math.pow(10, decplaces) to use 2 instead but to no avail. What do you think?

Thanks as always,

DES >:):O>
 
i use this:
Code:
//function to make a character Array. included for the currency function
String.prototype.toArray = function()
{
	var charAr = new Array();
	for(var i = 0;i < this.length;i++)
	{
		charAr[i] = this.charAt(i);
	}
	return charAr;
}

//reverse function for strings. included for the currency function.
String.prototype.reverse = function()
{
	var str, charAr;
	charAr = new Array();
	charAr = this.toArray();
	charAr = charAr.reverse();
	str = charAr.join(&quot;&quot;);
	return str;
}

//function to turn a number into a formatted string in US currency format.
Number.prototype.toUSCurrency = function()
{
	var str, dec, x, y;
	str = Math.round(this * 100)/100;
	str = new String(str);
	dec = (str.indexOf(&quot;.&quot;) > -1)?str.split(&quot;.&quot;)[1]:&quot;00&quot;;
	str = (str.indexOf(&quot;.&quot;) > -1)?str.split(&quot;.&quot;)[0]:str;
	x = str.length;
	str = str.reverse();
	y = &quot;&quot;;
	for(var i = 0; i < x; i++)
	{
		y += (i%3 == 0 && i != 0 && i != x)?&quot;,&quot;:&quot;&quot;;
		y += str.charAt(i)
	}
	y = y.reverse();
	return &quot;$&quot;+y+&quot;.&quot;+dec;
}

then you just use it like so:

var x = 3.4756;
x = x.toUSCurrency(); luciddream@subdimension.com
 
Thanks Lucid,

That is quite a relief for me... Could you just ellaborate on what goes where? I think you mean: x = x.toUSCurrency(); is attached to the field? how does it work for the entire spreadsheet?

DES
 
<script>
String.prototype.toArray = function()
{
var charAr = new Array();
for(var i = 0;i < this.length;i++)
{
charAr = this.charAt(i);
}
return charAr;
}

//reverse function for strings. included for the currency function.
String.prototype.reverse = function()
{
var str, charAr;
charAr = new Array();
charAr = this.toArray();
charAr = charAr.reverse();
str = charAr.join(&quot;&quot;);
return str;
}

//function to turn a number into a formatted string in US currency format.
Number.prototype.toUSCurrency = function()
{
var str, dec, x, y;
str = Math.round(this * 100)/100;
str = new String(str);
dec = (str.indexOf(&quot;.&quot;) > -1)?str.split(&quot;.&quot;)[1]:&quot;00&quot;;
str = (str.indexOf(&quot;.&quot;) > -1)?str.split(&quot;.&quot;)[0]:str;
x = str.length;
str = str.reverse();
y = &quot;&quot;;
for(var i = 0; i < x; i++)
{
y += (i%3 == 0 && i != 0 && i != x)?&quot;,&quot;:&quot;&quot;;
y += str.charAt(i)
}
y = y.reverse();
return &quot;$&quot;+y+&quot;.&quot;+dec;
}

function add(field1, field2, field3)
{
var x = parseInt(field1.value);
var y = parseInt(field2.value);
var z = x + y;
field3.value = z.toUSCurrency();
}
</script>
<input type=&quot;text&quot; id=&quot;ben&quot;><br>
<input type=&quot;text&quot; id=&quot;luc&quot;><br>
<input type=&quot;text&quot; id=&quot;chuck&quot;><br>
<button onclick=&quot;add(ben, luc, chuck)&quot;>add()</button>
luciddream@subdimension.com
 
Hi again,

Sorry to say that it's returning an error: 'stack overflow at line: 43' I dont know why that is.... So just to try and simplify ill show you my entire code. This way you may see my problem and know forsure. Take care

DES


<code>
<HTML>

<head><title>INVOICE</title>
<link rel=&quot;stylesheet&quot; href=&quot;stylesheet.css&quot;>
<script language=&quot;javascript&quot;>

function calculate(what){
what.prompt20.value = &quot;0.07&quot;
// Total ALL cells
what.prompt19.value = (what.prompt10.value - 0) + (what.prompt14.value - 0) + (what.prompt18.value - 0);
what.prompt20.value = (what.prompt19.value - 0) * (what.prompt20.value - 0 );
what.prompt21.value = (what.prompt19.value - 0) + (what.prompt20.value - 0) ;
}

function removeTax(what){
if(invoice.noGst.checked){
invoice.prompt20.value=&quot;0&quot;;
what.prompt21.value = (what.prompt19.value - 0) + (what.prompt20.value - 0);
}
else invoice.prompt20.value = 0.07;
}

function subtract(what){
//subtract
what.prompt23.value = (what.prompt21.value - 0) - (what.prompt22.value - 0);
}

</script>
<script language=&quot;javascript&quot;>

String.prototype.toArray = function(){
var charAr = new Array();
for(var i = 0;i < this.length;i++){
charAr = this.charAt(i);
}
return charAr;
}

//reverse function for strings. included for the currency function.
String.prototype.reverse = function(){
var str, charAr;
charAr = new Array();
charAr = this.toArray();
charAr = charAr.reverse();
str = charAr.join(&quot;&quot;);
return str;
}

//function to turn a number into a formatted string in US currency format.
Number.prototype.toUSCurrency = function(){
var str, dec, x, y;
str = Math.round(this * 100)/100;
str = new String(str);
dec = (str.indexOf(&quot;.&quot;) > -1)?str.split(&quot;.&quot;)[1]:&quot;00&quot;;
str = (str.indexOf(&quot;.&quot;) > -1)?str.split(&quot;.&quot;)[0]:str;
x = str.length;
str = str.reverse();
y = &quot;&quot;;
for(var i = 0; i < x; i++){
y += (i%3 == 0 && i != 0 && i != x)?&quot;,&quot;:&quot;&quot;;
y += str.charAt(i)
}
y = y.reverse();
return &quot;$&quot;+y+&quot;.&quot;+dec;
}

function add(field1, field2, field3){
var x = parseInt(field1.value);
var y = parseInt(field2.value);
var z = x + y;
field3.value = z.toUSCurrency();
}
</script>
</head>

<body width=&quot;600&quot; onLoad=&quot;document.invoice.prompt0.focus()&quot;>

<table width=&quot;100%&quot; bgcolor=&quot;#000000&quot; >
<tr>
<td width align class=&quot;white&quot;>INVOICE</td>
</tr>
</table>
<BR><BR>
<img src=&quot;dropShadow.gif&quot; border=&quot;0&quot; align=&quot;right&quot;>


<FORM name=&quot;invoice&quot; action method=&quot;post&quot; onSubmit>

<table width=&quot;500&quot; align=&quot;left&quot; border=&quot;0&quot; cellpadding=&quot;1&quot; cellspacing class=&quot;tiny&quot;>
<tr>
<td width=&quot;100&quot;><B>Invoice Number:</b></td>
<td width=&quot;&quot; align><input type=&quot;text&quot; name=&quot;prompt0&quot; ></td>
</tr>
<tr>
<td width><b>Member Number:</b></td>
<td width align><input type=&quot;text&quot; name=&quot;prompt1&quot;></td>
</tr>
<tr>
<td width ><b>Date:</b></td>
<td width align><input type=&quot;text&quot; name=&quot;prompt2&quot;><script language=&quot;Javascript&quot;>
var today = new Date()
var month = today.getMonth() + 1
var day = today.getDate()
var year = today.getFullYear()
var s = &quot;/&quot;
document.invoice.prompt2.value = month + s + day + s + year
</script></td>
</tr>
<tr>
<td width=&quot;&quot; align valign=&quot;top&quot;><b>To:</b></td>
<td width align valign><textarea cols=&quot;35&quot; rows=&quot;5&quot; name=&quot;prompt3&quot;></textarea></td>
</tr>
</table>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<HR>
<BR>

<table width=&quot;600&quot; align cellpadding cellspacing=&quot;5&quot; class=&quot;small&quot;>
<TR>
<td width=&quot;300&quot; align><b>Invoice Date:</B></td>
<td width align><input type=&quot;text&quot; size=&quot;20&quot; name=&quot;prompt4&quot;></td>
</tr>
<tr>
<td width align valign=&quot;top&quot;><b>Invoice Issued By:</B></TD>
<td width align><select multiple width=&quot;&quot; size=&quot;1&quot; name=&quot;prompt5&quot;>
<option name=&quot;CEO&quot;>CEO (Dan Stapleton)
<option name=&quot;Reg&quot;>Registrar (Carole Farr)
<option name=&quot;Dir1&quot;>Director, Finance & Administration (Janet Crofton)
<Option name=&quot;Dir2&quot;>Director, Events & Sponsorships (Marta Pawych)
<option name=&quot;Comm&quot;>Director, Communications (Beverley, Allen)
<Option name=&quot;AR&quot;>Assistant Registrar (Peter Ruggiero)
<option name=&quot;Ops&quot;>Operations Manager (Katia Tam)
<option name=&quot;Fin&quot;>Finance Manager (Gary Monk)
<option name=&quot;IT&quot;>I.S. Manager (Nick Gao)
<option name=&quot;PRM&quot;>Project Manager (Alicia Tessier)
<option name=&quot;DCO&quot;>Member Services Associate (Kim Kuan)
<option name=&quot;Rec&quot;>Reception (Pat Collins)
<option name=&quot;mem&quot;>Membership Coordinator (Arlene Herscheid)
<option name=&quot;EA&quot;>Executive Assistant (Louise Tagliacozzo)
<option name=&quot;chp&quot;>Chapter Relations Coordinator (Lesley Henderson)
<option name=&quot;Pro&quot;>Programs & Exhibits Coordinator (Sherrydawn Warren)
<option name=&quot;Eve&quot;>Events & Sponsorships Assistant (Carla Leszkowicz)
<option name=&quot;res&quot;>Resource Center Supervisor (Gina Matesic)
<option name=&quot;re&quot;>Resource Center Supervisor (Helena Merrian)
<option name=&quot;CO&quot;>Communications Associate (Jennifer Noddle)
<option name=&quot;Se&quot;>Senior Writer, Managing Editor, HR Professional (Kathleen Rowlands)
<option name=&quot;Adm&quot;>Administrative Assistant (Catherine Thomas-Keating)
<option name=&quot;Acc&quot;>Accounting Coordinator (David Ward)
<option name=&quot;eap&quot;>P.S.C. - Evening Academic Programs (Margaret Carter)
<option name=&quot;cert&quot;>P.S.C. - Certification (Beth Dennis)
<option name=&quot;pm&quot;>P.S.C. - Membership (Duane McKenzie)
<option name=&quot;cpe&quot;>P.S.C. - C.P.E. (Kelly Morris)
</select>
</td>
</tr>
<tr>
<td width align><B>Tax Registration #:</B></TD>
<td width align><input type=&quot;text&quot; size=&quot;20&quot; name=&quot;prompt6&quot; value=&quot;R104154273&quot;></td>
</tr>
</table>
<br>
<br>
<br>

<TABLE WIDTH=&quot;650&quot; BORDER=&quot;0&quot; ALIGN=&quot;center&quot; CLASS=&quot;tiny&quot;>
<TR>
<TD WIDTH=&quot;100&quot; ALIGN=&quot;center&quot; VALIGN BGCOLOR=&quot;#F1F1F1&quot;><b>Unit</b></td>
<TD WIDTH=&quot;300&quot; ALIGN=&quot;center&quot; VALIGN BGCOLOR=&quot;#F1F1F1&quot;><b>Description</b></td>
<TD WIDTH=&quot;150&quot; ALIGN=&quot;center&quot; VALIGN BGCOLOR=&quot;#F1F1F1&quot;><b>Unit Price</b></td>
<TD WIDTH=&quot;100&quot; ALIGN=&quot;center&quot; VALIGN BGCOLOR=&quot;#F1F1F1&quot;><b>Total</b></td>
</TR>
<TR>
<TD WIDTH ALIGN=&quot;center&quot; VALIGN><INPUT TYPE=&quot;text&quot; NAME=&quot;prompt7&quot; onKeyUp=&quot;this.form.prompt10.value = (this.form.prompt7.value - 0) * (this.form.prompt9.value - 0);calculate(this.form);dollarize(this.form)&quot; size=&quot;10&quot;></TD>
<TD WIDTH ALIGN=&quot;center&quot; VALIGN><input type=&quot;text&quot; name=&quot;prompt8&quot; size=&quot;30&quot; value></td>
<TD WIDTH ALIGN=&quot;center&quot; VALIGN>$<INPUT TYPE=&quot;text&quot; NAME=&quot;prompt9&quot; onKeyUp=&quot;this.form.prompt10.value = (this.form.prompt7.value - 0) * (this.form.prompt9.value - 0);calculate(this.form)&quot; size=&quot;10&quot;></TD>
<TD WIDTH ALIGN=&quot;center&quot; VALIGN>$<input type=&quot;text&quot; name=&quot;prompt10&quot; size=&quot;10&quot; READONLY> </td>
</TR>
<TR>
<TD WIDTH ALIGN=&quot;center&quot; VALIGN><INPUT TYPE=&quot;text&quot; NAME=&quot;prompt11&quot; onKeyUp=&quot;this.form.prompt14.value = (this.form.prompt11.value - 0) * (this.form.prompt13.value - 0);calculate(this.form)&quot; size=&quot;10&quot;></TD>
<TD WIDTH ALIGN=&quot;center&quot; VALIGN><input type=&quot;text&quot; name=&quot;prompt12&quot; size=&quot;30&quot; value></td>
<TD WIDTH ALIGN=&quot;center&quot; VALIGN>$<INPUT TYPE=&quot;text&quot; NAME=&quot;prompt13&quot; onKeyUp=&quot;this.form.prompt14.value = (this.form.prompt11.value - 0) * (this.form.prompt13.value - 0);calculate(this.form)&quot; size=&quot;10&quot;></TD>
<TD WIDTH ALIGN=&quot;center&quot; VALIGN>$<input type=&quot;text&quot; name=&quot;prompt14&quot; size=&quot;10&quot; READONLY> </td>
</TR>
<TR>
<TD WIDTH ALIGN=&quot;center&quot; VALIGN><INPUT TYPE=&quot;text&quot; NAME=&quot;prompt15&quot; onKeyUp=&quot;this.form.prompt18.value = (this.form.prompt15.value - 0) * (this.form.prompt17.value - 0);calculate(this.form)&quot; size=&quot;10&quot;></TD>
<TD WIDTH ALIGN=&quot;center&quot; VALIGN><input type=&quot;text&quot; name=&quot;prompt16&quot; size=&quot;30&quot; value></td>
<TD WIDTH ALIGN=&quot;center&quot; VALIGN>$<INPUT TYPE=&quot;text&quot; NAME=&quot;prompt17&quot; onKeyUp=&quot;this.form.prompt18.value = (this.form.prompt15.value - 0) * (this.form.prompt17.value - 0);calculate(this.form)&quot; size=&quot;10&quot;></TD>
<TD WIDTH ALIGN=&quot;center&quot; VALIGN>$<input type=&quot;text&quot; name=&quot;prompt18&quot; size=&quot;10&quot; READONLY> <hr> </td>
</TR>
<TR>
<TD COLSPAN=&quot;2&quot;></TD>
<TD width align=&quot;right&quot;>SUBTOTAL</TD>
<TD width ALIGN=&quot;center&quot;>$<INPUT TYPE=&quot;text&quot; name=&quot;prompt19&quot; size=&quot;10&quot; READONLY></td>
</TR>
<TR>
<TD COLSPAN=&quot;2&quot;></TD>
<TD width align=&quot;right&quot; >GST</TD>
<TD width ALIGN=&quot;center&quot;>$<INPUT TYPE=&quot;text&quot; name=&quot;prompt20&quot; value=&quot;&quot; size=&quot;10&quot; READONLY></td>
<TD width align=&quot;center&quot;><input type=&quot;checkbox&quot; name=&quot;noGst&quot; value onClick=&quot;removeTax(this.form); dollarize()&quot;></td>
</TR>
<TR>
<TD COLSPAN=&quot;2&quot;></TD>
<TD width align=&quot;right&quot; >TOTAL</TD>
<TD width ALIGN=&quot;center&quot;>$<INPUT TYPE=&quot;text&quot; name=&quot;prompt21&quot; size=&quot;10&quot; READONLY></td>
</TR>
<TR>
<TD COLSPAN=&quot;2&quot;></TD>
<TD width align=&quot;right&quot; >Less Amount Received</TD>
<TD width ALIGN=&quot;center&quot;>$<INPUT TYPE=&quot;text&quot; name=&quot;prompt22&quot; size=&quot;10&quot; onKeyUp=&quot;this.form.prompt23.value = (this.form.prompt21.value - 0) - (this.form.prompt22.value - 0); subtract(this.form); toUsCurrency(this.form)&quot; ></td>
</TR>
<TR>
<TD COLSPAN=&quot;2&quot;></TD>
<TD width align=&quot;right&quot; ><B>BALANCE DUE</B></TD>
<TD width ALIGN=&quot;center&quot;>$<INPUT TYPE=&quot;text&quot; name=&quot;prompt23&quot; size=&quot;10&quot; READONLY></td>
</TR>
</table>
<br><button onclick=&quot;add(prompt19, prompt20, prompt21)&quot;>add()</button>

<br>
<br>

<input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;View Report&quot;>

<br>
<br>
<br>
</form>
</body>
</HTML>
</code>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top