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!

format variable

Status
Not open for further replies.

scribbler

MIS
Feb 4, 2002
206
GB
After searching the web for answers but not getting anywhere. Can anyone please tell me how to format either my variable or my textbox preferably into currency or if simpler then into a 2 place decimal value. My code initially takes variables received from previous pages and one which is user inputted, removes the comma's from the received variables, adds the 2 together but I cannot get them to display as currency or a formatted value.

Ive seen, and tried a variety of solutions within this forum but as I know very little about JS and I probably havent understood the code Ive seen e.g. Fixed.to(2), currency etc.

The item Im trying to format is the last line 'txtCortsTotal'

Code:
function calcTotal() {
    var f = document.forms['form1'];
	f.elements['txtBroker'].value = (f.elements['txtBroker'].value).replace(/,/g, ''); //removes commas
    var BrokerComm = parseFloat(f.elements['txtBroker'].value);
	f.elements['CortsPriceWithOptionsHidden'].value = (f.elements['CortsPriceWithOptionsHidden'].value).replace(/,/g, ''); //removes commas
    var CortsTotalWithOptions = parseFloat(f.elements['CortsPriceWithOptionsHidden'].value);
    f.elements['[b]txtCortsTotal[/b]'].value = BrokerComm + CortsTotalWithOptions;
}
 
You were almost there when you guessed at "Fixed.to(2)":

Code:
f.elements['txtCortsTotal'].value = (BrokerComm + CortsTotalWithOptions).toFixed(2);

This will round numbers to 2 DP, for example:

Code:
alert(1234.5678.toFixed(2));

would alert "1234.57".

Hope this helps,
Dan


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks for the reply Dan. I presume Ive got something wrong with my syntax as when I tested the code I dont get the alert box popup nor does the textbox on my form contain anything at all now which is why I thought Id misunderstood the code I had previously tested that I found on the web. Any ideas ??

Code:
<SCRIPT LANGAUAGE="javaScript">
function calcTotal() {
    var f = document.forms['form1'];
	f.elements['txtBroker'].value = (f.elements['txtBroker'].value).replace(/,/g, ''); //removes commas
    var BrokerComm = parseFloat(f.elements['txtBroker'].value);
	f.elements['CortsPriceWithOptionsHidden'].value = (f.elements['CortsPriceWithOptionsHidden'].value).replace(/,/g, ''); //removes commas
    var CortsTotalWithOptions = parseFloat(f.elements['CortsPriceWithOptionsHidden'].value);
    vat test = BrokerComm + CortsTotalWithOptions
	alert(test.toFixed(2));
	f.elements['txtCortsTotal'].value = BrokerComm + CortsTotalWithOptions	
	
}
</script>
 
Copied and pasted from your code above:

va[red]t[/red] test = BrokerComm + CortsTotalWithOptions
alert(test.toFixed(2));

Lee
 
Well what can I say Lee other than thanks for point my typo out....... I am suitably embarrassed. I know very little about JS but thats no excuse for not reading through the code thoroughly. In my defence I did start at 5:45am and after trying various options incorreclty assumed the problem was elsewhere.

Thanks for that...
 
I've done the EXACT same typo more times than I care to count since I started working with JS in 1999. Since the alert didn't fire, I knew the problem was somewhere before that, and started looking in reverse order at the previous lines.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top