Solved

Convert Currency to Number.

Posted on 2000-03-10
10
458 Views
Last Modified: 2007-12-19
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>
<head>
      <title></title>
</head>

<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
Comment
Question by:nathans
  • 7
  • 2
10 Comments
 
LVL 8

Expert Comment

by:xabi
ID: 2606353
Hre goes a working code:

<html>
<head>
<title></title>
</head>

<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

by:
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

by:brigmar
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

by:nathans
ID: 2606849
Adjusted points to 300
0
 
LVL 5

Author Comment

by:nathans
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 5

Author Comment

by:nathans
ID: 2606856
If they put in 3.25  I want it to be $3.25 yours =$3.00
0
 
LVL 5

Author Comment

by:nathans
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

by:nathans
ID: 2606872
Adjusted points to 350
0
 
LVL 5

Author Comment

by:nathans
ID: 2606873
What about Negative Numbers... How can I account for that.
0
 
LVL 5

Author Comment

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


-->


<html>
<head>
<title></title>
</head>

<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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
date format angularjs2 16 40
jQuery Animate Left Right 2 44
HTML page and JavaScript 2 52
jQuery animation faster 1 16
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

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

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

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now