?
Solved

how to sum two numbers in c# page using javascript

Posted on 2010-11-19
9
Medium Priority
?
489 Views
Last Modified: 2012-05-10

i want to add two numbers that are generated dynamically which contain symbols like dollar and comma.

ex:
 [txtField1] + [txtField2] =  [txtsum]
$1,111.11 + $1,111.11 = 2222.22

function calsum()
{
   var objField1 = document.getElementByUD('txtField1');
   var objField2 = document.getElementByUD('txtField2');

  var sum1 = parseFloat(objField1.value) + parseFloat(objField2.value);

   document.getElementByUD('txtsum')  = sum1.value;

}
0
Comment
Question by:Bharat Guru
9 Comments
 
LVL 3

Expert Comment

by:brd24gor
ID: 34175078
You can use String.Replace to filter out commas and dollar signs, do the float conversion, then add them up.

txtField1.Replace("$","");
txtField2.Replace(",","");
0
 
LVL 27

Expert Comment

by:Shaun Kline
ID: 34175089
In javascript, you would use:

var sum1 = parseFloat(objField1.value.replace(/\$|\,/g,'')) + parseFloat(objField2.value.replace(/\$|\,/g,''));
0
 

Author Comment

by:Bharat Guru
ID: 34175663
when txtField1 or txtField2 is null then result is shown as NaN.  
when field1 or field2 are null then result should be ''  (empty)
0
Industry Leaders: 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!

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34176016
Try this out:
function calsum()
{
	var objField1 = document.getElementById('txtField1').value.replace(/[^0-9.]/g, '');
	var objField2 = document.getElementById('txtField2').value.replace(/[^0-9.]/g, '');

	var sum1 = (objField1 == null ? 0 : parseFloat(objField1)) + (objField2 == null ? 0 : parseFloat(objField2));

	document.getElementById('txtsum').value = sum1;
}

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34176114
This would be a bit safer, I think:
function calsum()
{
	var objField1 = document.getElementById('txtField1').value.replace(/[^0-9.]|\.(?=\d*\.)/g, '');
	var objField2 = document.getElementById('txtField2').value.replace(/[^0-9.]|\.(?=\d*\.)/g, '');

	var sum1 = (objField1 == null ? 0 : parseFloat(objField1)) + (objField2 == null ? 0 : parseFloat(objField2));

	document.getElementById('txtsum').value = sum1;
}

Open in new window

0
 

Author Comment

by:Bharat Guru
ID: 34176387
minus addition doesn't work
 5   -5   = shows 10 instead of 0
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 34176565
Silly me...


Corrected:
function calsum()
{
	var objField1 = document.getElementById('txtField1').value.replace(/[^0-9.-]|(\d)-|\.(?=\d*\.)/g, '$1');
	var objField2 = document.getElementById('txtField2').value.replace(/[^0-9.-]|(\d)-|\.(?=\d*\.)/g, '$1');

	var sum1 = (objField1 == null ? 0 : parseFloat(objField1)) + (objField2 == null ? 0 : parseFloat(objField2));

	document.getElementById('txtsum').value = sum1;
}

Open in new window

0
 

Author Closing Comment

by:Bharat Guru
ID: 34176663
Thanks a lot perfect
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34176681
NP. Glad to help  :)
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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

840 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