[Last Call] Learn how to a build a cloud-first strategyRegister Now

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

javascript subtract to dollar values

$a = document.getElementByID('dollar1');  //  returns $2,000,000.50;
$b = document.getElementByID('dollar2');  //  returns $1,000,000.10;

How do I get $a - $b to return $1,000,000.40;

I've tried parseFloat($a) - parseFloat($b)
I've tried parseFloat($a.replace('$', '')) - parseFloat($a.replace('$', ''))

thanks
0
joomla
Asked:
joomla
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
Like this...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>JS Math</title>
</head>
<body>
<h1>JS Math</h1>
<script type="text/javascript">
<!--
var d1 = 2000000.50;
var d2 = 1000000.10;
document.write(d1.toFixed(2)+"<br>");
document.write(d2.toFixed(2)+"<br>");
var d3;
d3 = d1-d2;
document.write(d3.toFixed(2)+"<br>");

// -->
</script>
</body>
</html>

Open in new window

0
 
joomlaAuthor Commented:
sorry, I don't understand what you're suggesting.

I have a form with two three fields
If I enter the value $2,000,0000.50 into field called "dollar1"
and
enter the value $1,000,000.10 into a field called "dollar2"

I want an onchange routine in field "dollar2" to populate a field called "dollar3"
with the value $1,000,000.40

thanks
0
 
Dave BaldwinFixer of ProblemsCommented:
The reason you don't often see numbers formatted like that is that javascript doesn't understand the commas ','.  If you enter numbers that way, you have to strip off the '$' and the ',' before you try to do the math or it won't work.  Then to display the result that way, you have to add those back into the result.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
joomlaAuthor Commented:
thanks,
i'm sorry to be ackward, but I did try strip the $ and the , from the numbers but keep getting an error.

can you suggest a work reg pattern ?
0
 
aplusexpertCommented:
Try this script.

<script type="text/javascript">
function getVal(){

var dollar1= parseInt(document.getElementById('dollar1').value.replace('$', ''));
var doller2= parseInt(document.getElementById('dollar2').value.replace('$', ''));

var ans= doller1- doller2;

alert(output);
}
</script>

Open in new window


I think this may helps you.
If not then refer this link
http://www.webdeveloper.com/forum/showthread.php?t=189831

Thanks...
0
 
Michel PlungjanIT ExpertCommented:
1) remove $ and ALL commas
2) subtract the numbers
3) reformat the numbers

http://www.mredkj.com/javascript/numberFormat.html#addcommas


var diff = parseFloat($a.replace(/[,$]/g, '')) - parseFloat($b.replace(/[,$]/g, ""))
var res = "$"+addCommas(diff);
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.

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