[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 500
  • Last Modified:

Convert Currency to Number.

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
nathans
Asked:
nathans
  • 7
  • 2
1 Solution
 
xabiCommented:
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
 
xabiCommented:
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
 
brigmarCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nathansAuthor Commented:
Adjusted points to 300
0
 
nathansAuthor Commented:
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
 
nathansAuthor Commented:
If they put in 3.25  I want it to be $3.25 yours =$3.00
0
 
nathansAuthor Commented:
I solved most of it now help me with making sure they only type in

$1234567890.,

The Characters above

xabi?

0
 
nathansAuthor Commented:
Adjusted points to 350
0
 
nathansAuthor Commented:
What about Negative Numbers... How can I account for that.
0
 
nathansAuthor Commented:
<!--
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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now