Solved

# Convert Currency to Number.

Posted on 2000-03-10
470 Views
I want to Convert Currency to a Number.

I want to display a Currency in a text field in a form then when I change another field I want to be able to add these fields together. So if it has a \$ in it then it will be a string.  Here is a sample of my WORKING code.  Make it where all fields show up with \$ in front ex:\$0.00 and when I change the field to 2.25 I want the field to show \$2.25 and the total to add \$2.25 + \$0.00.

Here is some code.
========================

<html>
<title></title>

<body>
<!-- ----------------- JavaScript ----------------- -->

<SCRIPT LANGUAGE="JavaScript" SRC="/pams/includes/currency.js"></SCRIPT>

<script language="JavaScript"><!--

function UpdateTotals(data) {

total3  = eval(document.forms.ItemDetails.Jan3.value);
total3 += eval(document.forms.ItemDetails.Feb3.value);
document.forms.ItemDetails.total3.value = currency(total3);

total4  = eval(document.forms.ItemDetails.Jan4.value);
total4 += eval(document.forms.ItemDetails.Feb4.value);
document.forms.ItemDetails.total4.value = currency(total4);

return true;
}
-->
</script>

<form action="t" name="ItemDetails" method="post">
<table>
<tr>
<td align="right">Jan:</td>
<td><input type="text" name="Jan3" size="15" maxlength="30" value=0.00 style="text-align: right;" onchange="UpdateTotals(this.value)"></td>
<td><input type="text" name="Jan4" size="15" maxlength="30" value=0.00 style="text-align: right;" onchange="UpdateTotals(this.value)"></td>
</tr>
<tr>
<td align="right">Feb:</td>
<td><input type="text" name="Feb3" size="15" maxlength="30" value=0.00 style="text-align: right;" onchange="UpdateTotals(this.value)"></td>
<td><input type="text" name="Feb4" size="15" maxlength="30" value=0.00 style="text-align: right;" onchange="UpdateTotals(this.value)"></td>
</tr>
<tr>
<td align="right">Totals:</td>
<td><input type="text" name="total3" size="15" maxlength="30" value=0.00 style="text-align: right;"></td>
<td><input type="text" name="total4" size="15" maxlength="30" value=0.00 style="text-align: right;"></td>
</tr>
</table>

</form>

</body>
</html>
0
Question by:nathans
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
• 7
• 2

LVL 8

Expert Comment

ID: 2606353
Hre goes a working code:

<html>
<title></title>

<body>
<!-- ----------------- JavaScript ----------------- -->

<script language="JavaScript"><!--

function valid_number(string) {
var sTmp = ""
var sValid = "0123456789."
for (var i=0; i< string.length; i++) {
if (sValid.indexOf(string.charAt(i)) != -1) sTmp += string.charAt(i)
}
return(sTmp)
}

function UpdateTotals(data) {

total3  = parseInt(valid_number(document.ItemDetails.Jan3.value))
total3 += parseInt(valid_number(document.ItemDetails.Feb3.value))
document.ItemDetails.total3.value = total3;
//document.forms.ItemDetails.total3.value = currency(total3);

total4  = parseInt(valid_number(document.ItemDetails.Jan4.value))
total4 += parseInt(valid_number(document.ItemDetails.Feb4.value))
document.ItemDetails.total4.value = total4
//document.forms.ItemDetails.total4.value = currency(total4);

return true;
}
-->
</script>

<form action="t" name="ItemDetails" method="post">
<table>
<tr>
<td align="right">Jan:</td>
<td><input type="text" name="Jan3" size="15" maxlength="30" value=0.00 style="text-align: right;" onchange="UpdateTotals()"></td>
<td><input type="text" name="Jan4" size="15" maxlength="30" value=0.00 style="text-align: right;" onchange="UpdateTotals()"></td>
</tr>
<tr>
<td align="right">Feb:</td>
<td><input type="text" name="Feb3" size="15" maxlength="30" value=0.00 style="text-align: right;" onchange="UpdateTotals()"></td>
<td><input type="text" name="Feb4" size="15" maxlength="30" value=0.00 style="text-align: right;" onchange="UpdateTotals()"></td>
</tr>
<tr>
<td align="right">Totals:</td>
<td><input type="text" name="total3" size="15" maxlength="30" value=0.00 style="text-align: right;"></td>
<td><input type="text" name="total4" size="15" maxlength="30" value=0.00 style="text-align: right;"></td>
</tr>
</table>

</form>

</body>
</html>

xabi
0

LVL 8

Accepted Solution

xabi earned 350 total points
ID: 2606362
a Few Notes:

- I removed the included javascript code cause I don't have that file. That's the cause I comment two lines of your code:

//document.forms.ItemDetails.total3.value = currency(total3);
and this other one:
//document.forms.ItemDetails.total4.value = currency(total4);

And used my own ones (the ones before the commented)

If you want the result with the dollar sign before the total just change my two added lines into this other ones:

document.ItemDetails.total3.value = "\$" + total3;
document.ItemDetails.total4.value = "\$" + total4

xabi

0

LVL 4

Expert Comment

ID: 2606363
nathans,

I have a few questions:
1 : Do you allow the user to put their own '\$' in if they want ?
2 : Do you allow negative numbers ?
3 : What do you want to do with entries that dont equate to a number (eg. if the user enters 'hippo') ?
4 : What is your target browser audience ? Do you want it to be compatible back to IE3 (IE3, NN3, NN4, IE4, IE5 ) ?

Brian
0

LVL 5

Author Comment

ID: 2606849
Adjusted points to 300
0

LVL 5

Author Comment

ID: 2606850
1. What if the user puts in .5
2. What if the user puts in hippo
3. I want all numbers to start and end up with \$0.00 format.

IE4, IE5, Netscape

xabi you are very close.

0

LVL 5

Author Comment

ID: 2606856
If they put in 3.25  I want it to be \$3.25 yours =\$3.00
0

LVL 5

Author Comment

ID: 2606863
I solved most of it now help me with making sure they only type in

\$1234567890.,

The Characters above

xabi?

0

LVL 5

Author Comment

ID: 2606872
Adjusted points to 350
0

LVL 5

Author Comment

ID: 2606873
What about Negative Numbers... How can I account for that.
0

LVL 5

Author Comment

ID: 2606918
<!--
Here is the working CODE.

-->

<html>
<title></title>

<body>
<!-- ----------------- JavaScript ----------------- -->
<script language="JavaScript"><!--
//-- JavaScript code written by Alan Simpson - www.coolnerds.com
function currency(anynum) {
//-- Returns passed number as string in \$xxx,xxx.xx format.
anynum=eval(anynum)
workNum=Math.abs((Math.round(anynum*100)/100));workStr=""+workNum
if (workStr.indexOf(".")==-1){workStr+=".00"}
dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0
pStr=workStr.substr(workStr.indexOf("."))
while (pStr.length<3){pStr+="0"}

//--- Adds comma in thousands place.
if (dNum>=1000) {
dLen=dStr.length
dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
}

//-- Adds comma in millions place.
if (dNum>=1000000) {
dLen=dStr.length
dStr=parseInt(""+(dNum/1000000))+","+dStr.substring(dLen-7,dLen)
}
retval = dStr + pStr
//-- Put numbers in parentheses if negative.
if (anynum<0) {retval="-"+retval}
return "\$"+retval
}
// --></script>

<script language="JavaScript"><!--

function valid_number(string) {
var sTmp = ""
var sValid = "-0123456789."
for (var i=0; i< string.length; i++) {
if (sValid.indexOf(string.charAt(i)) != -1) sTmp += string.charAt(i)
}
return(sTmp)
}

function UpdateTotals(data) {

total3  = parseFloat(valid_number(document.ItemDetails.Jan3.value))
total3 += parseFloat(valid_number(document.ItemDetails.Feb3.value))
document.ItemDetails.total3.value = currency(total3);
//document.forms.ItemDetails.total3.value = currency(total3);
document.ItemDetails.Jan3.value = currency(valid_number(document.ItemDetails.Jan3.value))
document.ItemDetails.Feb3.value = currency(valid_number(document.ItemDetails.Feb3.value))

total4  = parseFloat(valid_number(document.ItemDetails.Jan4.value))
total4 += parseFloat(valid_number(document.ItemDetails.Feb4.value))
document.ItemDetails.total4.value = currency(total4)
//document.forms.ItemDetails.total4.value = currency(total4);
document.ItemDetails.Jan4.value = currency(valid_number(document.ItemDetails.Jan4.value))
document.ItemDetails.Feb4.value = currency(valid_number(document.ItemDetails.Feb4.value))

return true;
}
-->
</script>

<form action="t" name="ItemDetails" method="post">

<table>
<tr>
<td align="right">Jan:</td>
<td>
<input type="text" name="Jan3" size="15" maxlength="30" value=\$0.00 style="text-align: right;" ONKEYPRESS="if(((event.keyCode < 44) || (event.keyCode > 46)) && ((event.keyCode < 48) || (event.keyCode > 57))) event.returnValue = false;" onchange="UpdateTotals()">
</td>
<td><input type="text" name="Jan4" size="15" maxlength="30" value=\$0.00 style="text-align: right;" ONKEYPRESS="if(((event.keyCode < 44) || (event.keyCode > 46)) && ((event.keyCode < 48) || (event.keyCode > 57))) event.returnValue = false;" onchange="UpdateTotals()"></td>
</tr>
<tr>
<td align="right">Feb:</td>
<td><input type="text" name="Feb3" size="15" maxlength="30" value=\$0.00 style="text-align: right;" ONKEYPRESS="if(((event.keyCode < 44) || (event.keyCode > 46)) && ((event.keyCode < 48) || (event.keyCode > 57))) event.returnValue = false;" onchange="UpdateTotals()"></td>
<td><input type="text" name="Feb4" size="15" maxlength="30" value=\$0.00 style="text-align: right;" ONKEYPRESS="if(((event.keyCode < 44) || (event.keyCode > 46)) && ((event.keyCode < 48) || (event.keyCode > 57))) event.returnValue = false;" onchange="UpdateTotals()"></td>
</tr>
<tr>
<td align="right">Totals:</td>
<td><input type="text" readonly disabled name="total3" size="15" maxlength="30" value=\$0.00 style="text-align: right;"></td>
<td><input type="text" readonly disabled name="total4" size="15" maxlength="30" value=\$0.00 style="text-align: right;"></td>
</tr>
</table>

</form>

</body>
</html>

<!---
Check out ColdFusion Tips Plus at
http://www.nsnd.com/cftips
or subscribe by sending a blank email to
subscribe-cftips@nsnd.com
--->
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
###### Suggested Courses
Course of the Month7 days, 2 hours left to enroll

#### 734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.